From nobody Thu Dec 18 14:12:56 2025 Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B88F2314D1F; Mon, 27 Oct 2025 17:14:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.112 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761585269; cv=pass; b=P5B+eCuQQ/N/2q8gbNAaKEfoOaz6ToGTDNzggATi0o2T3D2QCwwz0xRCxaYJi7RdM+zMw9SBZEPeQq7/+kAZBf6midfFVPXsyFM8XbloVvdbWs0lNM8MWODn5NRP4qQ1ehPohz1LD94fGJnG7TmD6v9fVvdbvpoo4mv7FliWz/E= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761585269; c=relaxed/simple; bh=A/4Yz4hhujrm8CR0ftfBmFXk6/qM4KRv16czO0V08GY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=A4TlesNASOi5FGMAjzQryqbo3LOduc+nw/xTRT3VcycMDEQbfss+TOkTkagbBq7KIchgoNyLFFU5DqDn0ckgdd4Ha5RS7LBChK/VH0ilEburNRQnuYGsbXCe8xvU0JkE07s8zI4rZO+ZX/p89IpYwFsjUqGuM/K4wpsPTrkvhU0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=nicolas.frattaroli@collabora.com header.b=j6itSMIs; arc=pass smtp.client-ip=136.143.188.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=nicolas.frattaroli@collabora.com header.b="j6itSMIs" ARC-Seal: i=1; a=rsa-sha256; t=1761585245; cv=none; d=zohomail.com; s=zohoarc; b=CPtXThCicam2Ac3DHsVuX+IK7fWAdHUfnD7cxH79w26WEUn9RSzkUdCDonUQuhQ9o0wMH2qTcHQ4+49YlC7is0E8lPZZVvrcQsKgfS81wsOlHx92U3GIHLPSoudoEe6goXw6Xpc53toaVrW9GcIfF+NK5H3IncGzUUVo8R3CMKQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761585245; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=WxeBMINNWAM7WRg6131Emc+P7krNwdR5I4H1Br/gtac=; b=dqgL2ftJLhq/pkLze3FfCbjbG69nJpRLzkP0b2fVE/F5wnk5v5dDJuB23O2nFx7ciBGEQVcPXQYfmxaF2BaFWHdrcJyBcpL1Cx88IwYfNbfEhBbOndRld0irbHsoQKwCBO1VxgZfyPZLspqqCmiAv8K1o2UT8olT5Mg6sT4mt88= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=nicolas.frattaroli@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1761585245; s=zohomail; d=collabora.com; i=nicolas.frattaroli@collabora.com; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=WxeBMINNWAM7WRg6131Emc+P7krNwdR5I4H1Br/gtac=; b=j6itSMIs4tSHT4qOrbkx1ThSOa5n7nWxEkBxy7kwoEs6tpKzcMo25S2sxXIjjx3f sGboq6AcyuB8q/2gcWVo/54ckBrfkRf3+/536m43oRr6lhbkBtZE6F3S0BxcF6N2pHV C2nurr6275WF3QbQ8j+8A0xcl0T0MOpo3AzTxmOE= Received: by mx.zohomail.com with SMTPS id 1761585243033285.30838624105627; Mon, 27 Oct 2025 10:14:03 -0700 (PDT) From: Nicolas Frattaroli Date: Mon, 27 Oct 2025 18:11:56 +0100 Subject: [PATCH v3 1/5] dt-bindings: pwm: Add a new binding for rockchip,rk3576-pwm 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: <20251027-rk3576-pwm-v3-1-654a5cb1e3f8@collabora.com> References: <20251027-rk3576-pwm-v3-0-654a5cb1e3f8@collabora.com> In-Reply-To: <20251027-rk3576-pwm-v3-0-654a5cb1e3f8@collabora.com> To: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Heiko Stuebner , Lee Jones , William Breathitt Gray Cc: kernel@collabora.com, Jonas Karlman , Alexey Charkov , linux-rockchip@lists.infradead.org, linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Nicolas Frattaroli , Conor Dooley X-Mailer: b4 0.14.3 The Rockchip RK3576 SoC has a newer PWM controller IP revision than previous Rockchip SoCs. This IP, called "PWMv4" by Rockchip, introduces several new features, and consequently differs in its bindings. Instead of expanding the ever-growing rockchip-pwm binding that already has an if-condition, add an entirely new binding to handle this. There are two additional clocks, "osc" and "rc". These are available for every PWM instance, and the PWM hardware can switch between the "pwm", "osc" and "rc" clock at runtime. The PWM controller also comes with an interrupt now. This interrupt is used to signal various conditions. Reviewed-by: Conor Dooley Reviewed-by: Rob Herring (Arm) Signed-off-by: Nicolas Frattaroli --- .../bindings/pwm/rockchip,rk3576-pwm.yaml | 77 ++++++++++++++++++= ++++ MAINTAINERS | 7 ++ 2 files changed, 84 insertions(+) diff --git a/Documentation/devicetree/bindings/pwm/rockchip,rk3576-pwm.yaml= b/Documentation/devicetree/bindings/pwm/rockchip,rk3576-pwm.yaml new file mode 100644 index 000000000000..48d5055c8b06 --- /dev/null +++ b/Documentation/devicetree/bindings/pwm/rockchip,rk3576-pwm.yaml @@ -0,0 +1,77 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/pwm/rockchip,rk3576-pwm.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Rockchip PWMv4 controller + +maintainers: + - Nicolas Frattaroli + +description: | + The Rockchip PWMv4 controller is a PWM controller found on several Rockc= hip + SoCs, such as the RK3576. + + It supports both generating and capturing PWM signals. + +allOf: + - $ref: pwm.yaml# + +properties: + compatible: + items: + - const: rockchip,rk3576-pwm + + reg: + maxItems: 1 + + clocks: + items: + - description: Used to derive the PWM signal. + - description: Used as the APB bus clock. + - description: Used as an alternative to derive the PWM signal. + - description: Used as another alternative to derive the PWM signal. + + clock-names: + items: + - const: pwm + - const: pclk + - const: osc + - const: rc + + interrupts: + maxItems: 1 + + "#pwm-cells": + const: 3 + +required: + - compatible + - reg + - clocks + - clock-names + - interrupts + +additionalProperties: false + +examples: + - | + #include + #include + #include + + soc { + #address-cells =3D <2>; + #size-cells =3D <2>; + + pwm@2add0000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x2add0000 0x0 0x1000>; + clocks =3D <&cru CLK_PWM1>, <&cru PCLK_PWM1>, <&cru CLK_OSC_PW= M1>, + <&cru CLK_RC_PWM1>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + #pwm-cells =3D <3>; + }; + }; diff --git a/MAINTAINERS b/MAINTAINERS index 252b06d4240c..baecabab35a2 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -22366,6 +22366,13 @@ F: Documentation/userspace-api/media/v4l/metafmt-r= kisp1.rst F: drivers/media/platform/rockchip/rkisp1 F: include/uapi/linux/rkisp1-config.h =20 +ROCKCHIP MFPWM +M: Nicolas Frattaroli +L: linux-rockchip@lists.infradead.org +L: linux-pwm@vger.kernel.org +S: Maintained +F: Documentation/devicetree/bindings/pwm/rockchip,rk3576-pwm.yaml + ROCKCHIP RK3568 RANDOM NUMBER GENERATOR SUPPORT M: Daniel Golle M: Aurelien Jarno --=20 2.51.1 From nobody Thu Dec 18 14:12:56 2025 Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A1464315D2A; Mon, 27 Oct 2025 17:14:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.112 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761585275; cv=pass; b=Osmsi8IKWug0EB4NW0ykwVdy3UxlW4MYn2AzHCPKa9jhP13PQfBPYsG2ICDrDQ/oItLCvzNR9zXA3aBqGfxE9H2yveZO22dx+bbZ0/BqCkja8eHLOVk4116fzUAYkL9wGDI7QwwJ34CwXDwx4viY6eKJ8asO8E5t6OMuPSQfHXg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761585275; c=relaxed/simple; bh=7rRPEjuwdg7Pv8NZfTlPDMpm6mMbwG5C8ojSid9v0Bo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ay4Q1VeyB1KccXaw+w2YxHzblWa/FfzOnGy83xM+6/YruHktflqBKOddw2Z8RSdrFkl4qLMpmzyZwi8bAr0f80qls0CAPnbkv6cKYpz1U8q0ovKUNQCU6WFkx41i3MyAICWvk0jvGVBZyoeIzqcLI2+dJEeoza7bi32vgQph520= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=nicolas.frattaroli@collabora.com header.b=HtXtJx9q; arc=pass smtp.client-ip=136.143.188.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=nicolas.frattaroli@collabora.com header.b="HtXtJx9q" ARC-Seal: i=1; a=rsa-sha256; t=1761585249; cv=none; d=zohomail.com; s=zohoarc; b=Va+pTORXouUNecI4uLcKNqPIggPvSOK8/R9dxkhA8xLd1nB1vGGEoyGbNWOI5nQy6Rk9X+ABnWD95dDr1+AOtvWI8ZSJiPu8nCj1Pz23PTQTZfNAflphreG29NU6vHlomo1uytiXjaO9IGTwsGSjeIMSLyTAlgDvpkXmARMby3A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761585249; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=NarRBoylhbPGxx+N1aPGbGQI8yC1tomNn2/zhbcMBUI=; b=iXxc8GzSrlGrUHnAgiToWcONQ12ZHoSeWnVh6bPXYVKx39fJHdehwBS5rNvEbVaqN2Fav1fbiZ3SDluMhqEuNUanvs97mbQF5QDo7/ppmc/Usm3MjmubLNRWHdg9pYBY91hLCRbaPQvBGqYBoNRz09EFPWjr+gjjN/bLw2VShYg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=nicolas.frattaroli@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1761585249; s=zohomail; d=collabora.com; i=nicolas.frattaroli@collabora.com; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=NarRBoylhbPGxx+N1aPGbGQI8yC1tomNn2/zhbcMBUI=; b=HtXtJx9qtO8su+JVKYTGu8Cu5L+Hxlm3+78nVLUetEzumArFJoGy8R7sUOj6RqmV C+Xg0LoKUlA6WRxYdPF4TQDz/8EolfUwQsYebDXZXZRhMZVxGcq0xwPckrqyZtyK0SO vgOH07ebunIsf3gb32PW6hH2GJYVkuoR9VbLEOxU= Received: by mx.zohomail.com with SMTPS id 1761585247322138.72995644389403; Mon, 27 Oct 2025 10:14:07 -0700 (PDT) From: Nicolas Frattaroli Date: Mon, 27 Oct 2025 18:11:57 +0100 Subject: [PATCH v3 2/5] mfd: Add Rockchip mfpwm 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: <20251027-rk3576-pwm-v3-2-654a5cb1e3f8@collabora.com> References: <20251027-rk3576-pwm-v3-0-654a5cb1e3f8@collabora.com> In-Reply-To: <20251027-rk3576-pwm-v3-0-654a5cb1e3f8@collabora.com> To: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Heiko Stuebner , Lee Jones , William Breathitt Gray Cc: kernel@collabora.com, Jonas Karlman , Alexey Charkov , linux-rockchip@lists.infradead.org, linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Nicolas Frattaroli X-Mailer: b4 0.14.3 With the Rockchip RK3576, the PWM IP used by Rockchip has changed substantially. Looking at both the downstream pwm-rockchip driver as well as the mainline pwm-rockchip driver made it clear that with all its additional features and its differences from previous IP revisions, it is best supported in a new driver. This brings us to the question as to what such a new driver should be. To me, it soon became clear that it should actually be several new drivers, most prominently when Uwe Kleine-K=C3=B6nig let me know that I should not implement the pwm subsystem's capture callback, but instead write a counter driver for this functionality. Combined with the other as-of-yet unimplemented functionality of this new IP, it became apparent that it needs to be spread across several subsystems. For this reason, we add a new MFD core driver, called mfpwm (short for "Multi-function PWM"). This "parent" driver makes sure that only one device function driver is using the device at a time, and is in charge of registering the MFD cell devices for the individual device functions offered by the device. An acquire/release pattern is used to guarantee that device function drivers don't step on each other's toes. Signed-off-by: Nicolas Frattaroli --- MAINTAINERS | 2 + drivers/mfd/Kconfig | 15 ++ drivers/mfd/Makefile | 1 + drivers/mfd/rockchip-mfpwm.c | 340 +++++++++++++++++++++++++++ include/linux/mfd/rockchip-mfpwm.h | 454 +++++++++++++++++++++++++++++++++= ++++ 5 files changed, 812 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index baecabab35a2..8f3235ba825e 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -22372,6 +22372,8 @@ L: linux-rockchip@lists.infradead.org L: linux-pwm@vger.kernel.org S: Maintained F: Documentation/devicetree/bindings/pwm/rockchip,rk3576-pwm.yaml +F: drivers/soc/rockchip/mfpwm.c +F: include/soc/rockchip/mfpwm.h =20 ROCKCHIP RK3568 RANDOM NUMBER GENERATOR SUPPORT M: Daniel Golle diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig index dbeac6825a10..8b3a3160fbdf 100644 --- a/drivers/mfd/Kconfig +++ b/drivers/mfd/Kconfig @@ -1367,6 +1367,21 @@ config MFD_RC5T583 Additional drivers must be enabled in order to use the different functionality of the device. =20 +config MFD_ROCKCHIP_MFPWM + tristate "Rockchip multi-function PWM controller" + depends on OF + depends on HAS_IOMEM + depends on COMMON_CLK + select MFD_CORE + help + Some Rockchip SoCs, such as the RK3576, use a PWM controller that has + several different functions, such as generating PWM waveforms but also + counting waveforms. + + This driver manages the overall device, and selects between different + functionalities at runtime as needed. Drivers for them are implemented + in their respective subsystems. + config MFD_RK8XX tristate select MFD_CORE diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile index e75e8045c28a..ebadbaea9e4a 100644 --- a/drivers/mfd/Makefile +++ b/drivers/mfd/Makefile @@ -231,6 +231,7 @@ obj-$(CONFIG_MFD_PALMAS) +=3D palmas.o obj-$(CONFIG_MFD_VIPERBOARD) +=3D viperboard.o obj-$(CONFIG_MFD_NTXEC) +=3D ntxec.o obj-$(CONFIG_MFD_RC5T583) +=3D rc5t583.o rc5t583-irq.o +obj-$(CONFIG_MFD_ROCKCHIP_MFPWM) +=3D rockchip-mfpwm.o obj-$(CONFIG_MFD_RK8XX) +=3D rk8xx-core.o obj-$(CONFIG_MFD_RK8XX_I2C) +=3D rk8xx-i2c.o obj-$(CONFIG_MFD_RK8XX_SPI) +=3D rk8xx-spi.o diff --git a/drivers/mfd/rockchip-mfpwm.c b/drivers/mfd/rockchip-mfpwm.c new file mode 100644 index 000000000000..08c2d8da41b7 --- /dev/null +++ b/drivers/mfd/rockchip-mfpwm.c @@ -0,0 +1,340 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2025 Collabora Ltd. + * + * A driver to manage all the different functionalities exposed by Rockchi= p's + * PWMv4 hardware. + * + * This driver is chiefly focused on guaranteeing non-concurrent operation + * between the different device functions, as well as setting the clocks. + * It registers the device function platform devices, e.g. PWM output or + * PWM capture. + * + * Authors: + * Nicolas Frattaroli + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * struct rockchip_mfpwm - private mfpwm driver instance state struct + * @pdev: pointer to this instance's &struct platform_device + * @base: pointer to the memory mapped registers of this device + * @pwm_clk: pointer to the PLL clock the PWM signal may be derived from + * @osc_clk: pointer to the fixed crystal the PWM signal may be derived fr= om + * @rc_clk: pointer to the RC oscillator the PWM signal may be derived from + * @chosen_clk: a clk-mux of pwm_clk, osc_clk and rc_clk + * @pclk: pointer to the APB bus clock needed for mmio register access + * @active_func: pointer to the currently active device function, or %NULL= if no + * device function is currently actively using any of the sh= ared + * resources. May only be checked/modified with @state_lock = held. + * @acquire_cnt: number of times @active_func has currently mfpwm_acquire(= )'d + * it. Must only be checked or modified while holding @state= _lock. + * @state_lock: this lock is held while either the active device function,= the + * enable register, or the chosen clock is being changed. + * @irq: the IRQ number of this device + */ +struct rockchip_mfpwm { + struct platform_device *pdev; + void __iomem *base; + struct clk *pwm_clk; + struct clk *osc_clk; + struct clk *rc_clk; + struct clk *chosen_clk; + struct clk *pclk; + struct rockchip_mfpwm_func *active_func; + unsigned int acquire_cnt; + spinlock_t state_lock; + int irq; +}; + +static atomic_t subdev_id =3D ATOMIC_INIT(0); + +static inline struct rockchip_mfpwm *to_rockchip_mfpwm(struct platform_dev= ice *pdev) +{ + return platform_get_drvdata(pdev); +} + +static int mfpwm_check_pwmf(const struct rockchip_mfpwm_func *pwmf, + const char *fname) +{ + struct device *dev =3D &pwmf->parent->pdev->dev; + + if (IS_ERR_OR_NULL(pwmf)) { + dev_warn(dev, "called %s with an erroneous handle, no effect\n", + fname); + return -EINVAL; + } + + if (IS_ERR_OR_NULL(pwmf->parent)) { + dev_warn(dev, "called %s with an erroneous mfpwm_func parent, no effect\= n", + fname); + return -EINVAL; + } + + return 0; +} + +__attribute__((nonnull)) +static int mfpwm_do_acquire(struct rockchip_mfpwm_func *pwmf) +{ + struct rockchip_mfpwm *mfpwm =3D pwmf->parent; + unsigned int cnt; + + if (mfpwm->active_func && pwmf->id !=3D mfpwm->active_func->id) + return -EBUSY; + + if (!mfpwm->active_func) + mfpwm->active_func =3D pwmf; + + if (!check_add_overflow(mfpwm->acquire_cnt, 1, &cnt)) { + mfpwm->acquire_cnt =3D cnt; + } else { + dev_warn(&mfpwm->pdev->dev, "prevented acquire counter overflow in %s\n", + __func__); + return -EOVERFLOW; + } + + dev_dbg(&mfpwm->pdev->dev, "%d acquired mfpwm, acquires now at %u\n", + pwmf->id, mfpwm->acquire_cnt); + + return clk_enable(mfpwm->pclk); +} + +int mfpwm_acquire(struct rockchip_mfpwm_func *pwmf) +{ + struct rockchip_mfpwm *mfpwm; + unsigned long flags; + int ret =3D 0; + + ret =3D mfpwm_check_pwmf(pwmf, "mfpwm_acquire"); + if (ret) + return ret; + + mfpwm =3D pwmf->parent; + dev_dbg(&mfpwm->pdev->dev, "%d is attempting to acquire\n", pwmf->id); + + if (!spin_trylock_irqsave(&mfpwm->state_lock, flags)) + return -EBUSY; + + ret =3D mfpwm_do_acquire(pwmf); + + spin_unlock_irqrestore(&mfpwm->state_lock, flags); + + return ret; +} +EXPORT_SYMBOL_NS_GPL(mfpwm_acquire, "ROCKCHIP_MFPWM"); + +__attribute__((nonnull)) +static void mfpwm_do_release(const struct rockchip_mfpwm_func *pwmf) +{ + struct rockchip_mfpwm *mfpwm =3D pwmf->parent; + + if (!mfpwm->active_func) + return; + + if (mfpwm->active_func->id !=3D pwmf->id) + return; + + /* + * No need to check_sub_overflow here, !mfpwm->active_func above catches + * this type of problem already. + */ + mfpwm->acquire_cnt--; + + if (!mfpwm->acquire_cnt) + mfpwm->active_func =3D NULL; + + clk_disable(mfpwm->pclk); +} + +void mfpwm_release(const struct rockchip_mfpwm_func *pwmf) +{ + struct rockchip_mfpwm *mfpwm; + unsigned long flags; + + if (mfpwm_check_pwmf(pwmf, "mfpwm_release")) + return; + + mfpwm =3D pwmf->parent; + + spin_lock_irqsave(&mfpwm->state_lock, flags); + mfpwm_do_release(pwmf); + dev_dbg(&mfpwm->pdev->dev, "%d released mfpwm, acquires now at %u\n", + pwmf->id, mfpwm->acquire_cnt); + spin_unlock_irqrestore(&mfpwm->state_lock, flags); +} +EXPORT_SYMBOL_NS_GPL(mfpwm_release, "ROCKCHIP_MFPWM"); + +/** + * mfpwm_register_subdev - register a single mfpwm_func + * @mfpwm: pointer to the parent &struct rockchip_mfpwm + * @name: sub-device name string + * + * Allocate a single &struct mfpwm_func, fill its members with appropriate= data, + * and register a new mfd cell. + * + * Returns: 0 on success, negative errno on error + */ +static int mfpwm_register_subdev(struct rockchip_mfpwm *mfpwm, + const char *name) +{ + struct rockchip_mfpwm_func *func; + struct mfd_cell cell =3D {}; + + func =3D devm_kzalloc(&mfpwm->pdev->dev, sizeof(*func), GFP_KERNEL); + if (IS_ERR(func)) + return PTR_ERR(func); + func->irq =3D mfpwm->irq; + func->parent =3D mfpwm; + func->id =3D atomic_inc_return(&subdev_id); + func->base =3D mfpwm->base; + func->core =3D mfpwm->chosen_clk; + cell.name =3D name; + cell.platform_data =3D func; + cell.pdata_size =3D sizeof(*func); + // cell.ignore_resource_conflicts =3D true; + // cell.resources =3D mfpwm->pdev->resource; + // cell.num_resources =3D mfpwm->pdev->num_resources; + + return devm_mfd_add_devices(&mfpwm->pdev->dev, func->id, &cell, 1, NULL, + 0, NULL); +} + +static int mfpwm_register_subdevs(struct rockchip_mfpwm *mfpwm) +{ + int ret; + + ret =3D mfpwm_register_subdev(mfpwm, "pwm-rockchip-v4"); + if (ret) + return ret; + + ret =3D mfpwm_register_subdev(mfpwm, "rockchip-pwm-capture"); + if (ret) + return ret; + + return 0; +} + +static int rockchip_mfpwm_probe(struct platform_device *pdev) +{ + struct device *dev =3D &pdev->dev; + struct rockchip_mfpwm *mfpwm; + char *clk_mux_name; + const char *mux_p_names[3]; + int ret =3D 0; + + mfpwm =3D devm_kzalloc(&pdev->dev, sizeof(*mfpwm), GFP_KERNEL); + if (IS_ERR(mfpwm)) + return PTR_ERR(mfpwm); + + mfpwm->pdev =3D pdev; + + spin_lock_init(&mfpwm->state_lock); + + mfpwm->base =3D devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(mfpwm->base)) + return dev_err_probe(dev, PTR_ERR(mfpwm->base), + "failed to ioremap address\n"); + + mfpwm->pclk =3D devm_clk_get_prepared(dev, "pclk"); + if (IS_ERR(mfpwm->pclk)) + return dev_err_probe(dev, PTR_ERR(mfpwm->pclk), + "couldn't get and prepare 'pclk' clock\n"); + + mfpwm->irq =3D platform_get_irq(pdev, 0); + if (mfpwm->irq < 0) + return dev_err_probe(dev, mfpwm->irq, "couldn't get irq 0\n"); + + mfpwm->pwm_clk =3D devm_clk_get_prepared(dev, "pwm"); + if (IS_ERR(mfpwm->pwm_clk)) + return dev_err_probe(dev, PTR_ERR(mfpwm->pwm_clk), + "couldn't get and prepare 'pwm' clock\n"); + + mfpwm->osc_clk =3D devm_clk_get_prepared(dev, "osc"); + if (IS_ERR(mfpwm->osc_clk)) + return dev_err_probe(dev, PTR_ERR(mfpwm->osc_clk), + "couldn't get and prepare 'osc' clock\n"); + + mfpwm->rc_clk =3D devm_clk_get_prepared(dev, "rc"); + if (IS_ERR(mfpwm->rc_clk)) + return dev_err_probe(dev, PTR_ERR(mfpwm->rc_clk), + "couldn't get and prepare 'rc' clock\n"); + + clk_mux_name =3D devm_kasprintf(dev, GFP_KERNEL, "%s_chosen", dev_name(de= v)); + if (!clk_mux_name) + return -ENOMEM; + + mux_p_names[0] =3D __clk_get_name(mfpwm->pwm_clk); + mux_p_names[1] =3D __clk_get_name(mfpwm->osc_clk); + mux_p_names[2] =3D __clk_get_name(mfpwm->rc_clk); + mfpwm->chosen_clk =3D clk_register_mux(dev, clk_mux_name, mux_p_names, + ARRAY_SIZE(mux_p_names), + CLK_SET_RATE_PARENT, + mfpwm->base + PWMV4_REG_CLK_CTRL, + PWMV4_CLK_SRC_SHIFT, PWMV4_CLK_SRC_WIDTH, + CLK_MUX_HIWORD_MASK, NULL); + ret =3D clk_prepare(mfpwm->chosen_clk); + if (ret) { + dev_err(dev, "failed to prepare PWM clock mux: %pe\n", + ERR_PTR(ret)); + return ret; + } + + platform_set_drvdata(pdev, mfpwm); + + ret =3D mfpwm_register_subdevs(mfpwm); + if (ret) { + dev_err(dev, "failed to register sub-devices: %pe\n", + ERR_PTR(ret)); + return ret; + } + + return ret; +} + +static void rockchip_mfpwm_remove(struct platform_device *pdev) +{ + struct rockchip_mfpwm *mfpwm =3D to_rockchip_mfpwm(pdev); + unsigned long flags; + + spin_lock_irqsave(&mfpwm->state_lock, flags); + + if (mfpwm->chosen_clk) { + clk_unprepare(mfpwm->chosen_clk); + clk_unregister_mux(mfpwm->chosen_clk); + } + + spin_unlock_irqrestore(&mfpwm->state_lock, flags); +} + +static const struct of_device_id rockchip_mfpwm_of_match[] =3D { + { + .compatible =3D "rockchip,rk3576-pwm", + }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, rockchip_mfpwm_of_match); + +static struct platform_driver rockchip_mfpwm_driver =3D { + .driver =3D { + .name =3D KBUILD_MODNAME, + .of_match_table =3D rockchip_mfpwm_of_match, + }, + .probe =3D rockchip_mfpwm_probe, + .remove =3D rockchip_mfpwm_remove, +}; +module_platform_driver(rockchip_mfpwm_driver); + +MODULE_AUTHOR("Nicolas Frattaroli "); +MODULE_DESCRIPTION("Rockchip MFPWM Driver"); +MODULE_LICENSE("GPL"); diff --git a/include/linux/mfd/rockchip-mfpwm.h b/include/linux/mfd/rockchi= p-mfpwm.h new file mode 100644 index 000000000000..78d9c3396f7e --- /dev/null +++ b/include/linux/mfd/rockchip-mfpwm.h @@ -0,0 +1,454 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (c) 2025 Collabora Ltd. + * + * Common header file for all the Rockchip Multi-function PWM controller + * drivers that are spread across subsystems. + * + * Authors: + * Nicolas Frattaroli + */ + +#ifndef __SOC_ROCKCHIP_MFPWM_H__ +#define __SOC_ROCKCHIP_MFPWM_H__ + +#include +#include +#include +#include +#include + +struct rockchip_mfpwm; + +/** + * struct rockchip_mfpwm_func - struct representing a single function driv= er + * + * @id: unique id for this function driver instance + * @base: pointer to start of MMIO registers + * @parent: a pointer to the parent mfpwm struct + * @irq: the shared IRQ gotten from the parent mfpwm device + * @core: a pointer to the clk mux that drives this channel's PWM + */ +struct rockchip_mfpwm_func { + int id; + void __iomem *base; + struct rockchip_mfpwm *parent; + int irq; + struct clk *core; +}; + +/* + * PWMV4 Register Definitions + * -------------------------- + * + * Attributes: + * RW - Read-Write + * RO - Read-Only + * WO - Write-Only + * W1T - Write high, Self-clearing + * W1C - Write high to clear interrupt + * + * Bit ranges to be understood with Verilog-like semantics, + * e.g. [03:00] is 4 bits: 0, 1, 2 and 3. + * + * All registers must be accessed with 32-bit width accesses only + */ + +#define PWMV4_REG_VERSION 0x000 +/* + * VERSION Register Description + * [31:24] RO | Hardware Major Version + * [23:16] RO | Hardware Minor Version + * [15:15] RO | Reserved + * [14:14] RO | Hardware supports biphasic counters + * [13:13] RO | Hardware supports filters + * [12:12] RO | Hardware supports waveform generation + * [11:11] RO | Hardware supports counter + * [10:10] RO | Hardware supports frequency metering + * [09:09] RO | Hardware supports power key functionality + * [08:08] RO | Hardware supports infrared transmissions + * [07:04] RO | Channel index of this instance + * [03:00] RO | Number of channels the base instance supports + */ + +#define PWMV4_REG_ENABLE 0x004 +/* + * ENABLE Register Description + * [31:16] WO | Write Enable Mask for the lower half of the register + * Set bit `n` here to 1 if you wish to modify bit `n >> 16`= in + * the same write operation + * [15:06] RO | Reserved + * [05:05] RW | PWM Channel Counter Read Enable, 1 =3D enabled + */ +#define PWMV4_CHN_CNT_RD_EN(v) FIELD_PREP_WM16(BIT(5), (v)) +/* + * [04:04] W1T | PWM Globally Joined Control Enable + * 1 =3D this PWM channel will be enabled by a global pwm en= able + * bit instead of the PWM Enable bit. + */ +#define PWMV4_GLOBAL_CTRL_EN(v) FIELD_PREP_WM16(BIT(4), (v)) +/* + * [03:03] RW | Force Clock Enable + * 0 =3D disabled, if the PWM channel is inactive then so is= the + * clock prescale module + */ +#define PWMV4_FORCE_CLK_EN(v) FIELD_PREP_WM16(BIT(3), (v)) +/* + * [02:02] W1T | PWM Control Update Enable + * 1 =3D enabled, commits modifications of _CTRL, _PERIOD, _= DUTY and + * _OFFSET registers once 1 is written to it + */ +#define PWMV4_CTRL_UPDATE_EN FIELD_PREP_WM16_CONST(BIT(2), 1) +/* + * [01:01] RW | PWM Enable, 1 =3D enabled + * If in one-shot mode, clears after end of operation + */ +#define PWMV4_EN_MASK BIT(1) +#define PWMV4_EN(v) FIELD_PREP_WM16(PWMV4_EN_MASK, \ + ((v) ? 1 : 0)) +/* + * [00:00] RW | PWM Clock Enable, 1 =3D enabled + * If in one-shot mode, clears after end of operation + */ +#define PWMV4_CLK_EN_MASK BIT(0) +#define PWMV4_CLK_EN(v) FIELD_PREP_WM16(PWMV4_CLK_EN_MASK, \ + ((v) ? 1 : 0)) +#define PWMV4_EN_BOTH_MASK (PWMV4_EN_MASK | PWMV4_CLK_EN_MASK) +static inline __pure bool rockchip_pwm_v4_is_enabled(unsigned int val) +{ + return (val & PWMV4_EN_BOTH_MASK); +} + +#define PWMV4_REG_CLK_CTRL 0x008 +/* + * CLK_CTRL Register Description + * [31:16] WO | Write Enable Mask for the lower half of the register + * Set bit `n` here to 1 if you wish to modify bit `n >> 16`= in + * the same write operation + * [15:15] RW | Clock Global Selection + * 0 =3D current channel scale clock + * 1 =3D global channel scale clock + */ +#define PWMV4_CLK_GLOBAL(v) FIELD_PREP_WM16(BIT(15), (v)) +/* + * [14:13] RW | Clock Source Selection + * 0 =3D Clock from PLL, frequency can be configured + * 1 =3D Clock from crystal oscillator, frequency is fixed + * 2 =3D Clock from RC oscillator, frequency is fixed + * 3 =3D Reserved + * NOTE: The purpose for this clock-mux-outside-CRU construc= t is + * to let the SoC go into a sleep state with the PWM + * hardware still having a clock signal for IR input, = which + * can then wake up the SoC. + */ +#define PWMV4_CLK_SRC_PLL 0x0U +#define PWMV4_CLK_SRC_CRYSTAL 0x1U +#define PWMV4_CLK_SRC_RC 0x2U +#define PWMV4_CLK_SRC_SHIFT 13 +#define PWMV4_CLK_SRC_WIDTH 2 +/* + * [12:04] RW | Scale Factor to apply to pre-scaled clock + * 1 <=3D v <=3D 256, v means clock divided by 2*v + */ +#define PWMV4_CLK_SCALE_F(v) FIELD_PREP_WM16(GENMASK(12, 4), (v)) +/* + * [03:03] RO | Reserved + * [02:00] RW | Prescale Factor + * v here means the input clock is divided by pow(2, v) + */ +#define PWMV4_CLK_PRESCALE_F(v) FIELD_PREP_WM16(GENMASK(2, 0), (v)) + +#define PWMV4_REG_CTRL 0x00C +/* + * CTRL Register Description + * [31:16] WO | Write Enable Mask for the lower half of the register + * Set bit `n` here to 1 if you wish to modify bit `n >> 16`= in + * the same write operation + * [15:09] RO | Reserved + * [08:06] RW | PWM Input Channel Selection + * By default, the channel selects its own input, but writin= g v + * here selects PWM input from channel v instead. + */ +#define PWMV4_CTRL_IN_SEL(v) FIELD_PREP_WM16(GENMASK(8, 6), (v)) +/* [05:05] RW | Aligned Mode, 0 =3D Valid, 1 =3D Invalid */ +#define PWMV4_CTRL_UNALIGNED(v) FIELD_PREP_WM16(BIT(5), (v)) +/* [04:04] RW | Output Mode, 0 =3D Left Aligned, 1 =3D Centre Aligned */ +#define PWMV4_LEFT_ALIGNED 0x0U +#define PWMV4_CENTRE_ALIGNED 0x1U +#define PWMV4_CTRL_OUT_MODE(v) FIELD_PREP_WM16(BIT(4), (v)) +/* + * [03:03] RW | Inactive Polarity for when the channel is either disabled= or + * has completed outputting the entire waveform in one-shot = mode. + * 0 =3D Negative, 1 =3D Positive + */ +#define PWMV4_POLARITY_N 0x0U +#define PWMV4_POLARITY_P 0x1U +#define PWMV4_INACTIVE_POL(v) FIELD_PREP_WM16(BIT(3), (v)) +/* + * [02:02] RW | Duty Cycle Polarity to use at the start of the waveform. + * 0 =3D Negative, 1 =3D Positive + */ +#define PWMV4_DUTY_POL_SHIFT 2 +#define PWMV4_DUTY_POL_MASK BIT(PWMV4_DUTY_POL_SHIFT) +#define PWMV4_DUTY_POL(v) FIELD_PREP_WM16(PWMV4_DUTY_POL_MASK, \ + (v)) +/* + * [01:00] RW | PWM Mode + * 0 =3D One-shot mode, PWM generates waveform RPT times + * 1 =3D Continuous mode + * 2 =3D Capture mode, PWM measures cycles of input waveform + * 3 =3D Reserved + */ +#define PWMV4_MODE_ONESHOT 0x0U +#define PWMV4_MODE_CONT 0x1U +#define PWMV4_MODE_CAPTURE 0x2U +#define PWMV4_MODE_MASK GENMASK(1, 0) +#define PWMV4_MODE(v) FIELD_PREP_WM16(PWMV4_MODE_MASK, (v)) +#define PWMV4_CTRL_COM_FLAGS (PWMV4_INACTIVE_POL(PWMV4_POLARITY_N) | \ + PWMV4_DUTY_POL(PWMV4_POLARITY_P) | \ + PWMV4_CTRL_OUT_MODE(PWMV4_LEFT_ALIGNED) | \ + PWMV4_CTRL_UNALIGNED(true)) +#define PWMV4_CTRL_CONT_FLAGS (PWMV4_MODE(PWMV4_MODE_CONT) | \ + PWMV4_CTRL_COM_FLAGS) +#define PWMV4_CTRL_CAP_FLAGS (PWMV4_MODE(PWMV4_MODE_CAPTURE) | \ + PWMV4_CTRL_COM_FLAGS) + +#define PWMV4_REG_PERIOD 0x010 +/* + * PERIOD Register Description + * [31:00] RW | Period of the output waveform + * Constraints: should be even if CTRL_OUT_MODE is CENTRE_AL= IGNED + */ + +#define PWMV4_REG_DUTY 0x014 +/* + * DUTY Register Description + * [31:00] RW | Duty cycle of the output waveform + * Constraints: should be even if CTRL_OUT_MODE is CENTRE_AL= IGNED + */ + +#define PWMV4_REG_OFFSET 0x018 +/* + * OFFSET Register Description + * [31:00] RW | Offset of the output waveform, based on the PWM clock + * Constraints: 0 <=3D v <=3D (PERIOD - DUTY) + */ + +#define PWMV4_REG_RPT 0x01C +/* + * RPT Register Description + * [31:16] RW | Second dimensional of the effective number of waveform + * repetitions. Increases by one every first dimensional tim= es. + * Value `n` means `n + 1` repetitions. The final number of + * repetitions of the waveform in one-shot mode is: + * `(first_dimensional + 1) * (second_dimensional + 1)` + * [15:00] RW | First dimensional of the effective number of waveform + * repetitions. Value `n` means `n + 1` repetitions. + */ + +#define PWMV4_REG_FILTER_CTRL 0x020 +/* + * FILTER_CTRL Register Description + * [31:16] WO | Write Enable Mask for the lower half of the register + * Set bit `n` here to 1 if you wish to modify bit `n >> 16`= in + * the same write operation + * [15:10] RO | Reserved + * [09:04] RW | Filter window number + * [03:01] RO | Reserved + * [00:00] RW | Filter Enable, 0 =3D disabled, 1 =3D enabled + */ + +#define PWMV4_REG_CNT 0x024 +/* + * CNT Register Description + * [31:00] RO | Current value of the PWM Channel 0 counter in pwm clock c= ycles, + * 0 <=3D v <=3D 2^32-1 + */ + +#define PWMV4_REG_ENABLE_DELAY 0x028 +/* + * ENABLE_DELAY Register Description + * [31:16] RO | Reserved + * [15:00] RW | PWM enable delay, in an unknown unit but probably cycles + */ + +#define PWMV4_REG_HPC 0x02C +/* + * HPC Register Description + * [31:00] RW | Number of effective high polarity cycles of the input wav= eform + * in capture mode. Based on the PWM clock. 0 <=3D v <=3D 2^= 32-1 + */ + +#define PWMV4_REG_LPC 0x030 +/* + * LPC Register Description + * [31:00] RW | Number of effective low polarity cycles of the input wave= form + * in capture mode. Based on the PWM clock. 0 <=3D v <=3D 2^= 32-1 + */ + +#define PWMV4_REG_BIPHASIC_CNT_CTRL0 0x040 +/* + * BIPHASIC_CNT_CTRL0 Register Description + * [31:16] WO | Write Enable Mask for the lower half of the register + * Set bit `n` here to 1 if you wish to modify bit `n >> 16`= in + * the same write operation + * [15:10] RO | Reserved + * [09:09] RW | Biphasic Counter Phase Edge Selection for mode 0, + * 0 =3D rising edge (posedge), 1 =3D falling edge (negedge) + * [08:08] RW | Biphasic Counter Clock force enable, 1 =3D force enable + * [07:07] W1T | Synchronous Enable + * [06:06] W1T | Mode Switch + * 0 =3D Normal Mode, 1 =3D Switch timer clock and measured = clock + * Constraints: "Biphasic Counter Mode" must be 0 if this is= 1 + * [05:03] RW | Biphasic Counter Mode + * 0x0 =3D Mode 0, 0x1 =3D Mode 1, 0x2 =3D Mode 2, 0x3 =3D M= ode 3, + * 0x4 =3D Mode 4, 0x5 =3D Reserved + * [02:02] RW | Biphasic Counter Clock Selection + * 0 =3D clock is from PLL and frequency can be configured + * 1 =3D clock is from crystal oscillator and frequency is f= ixed + * [01:01] RW | Biphasic Counter Continuous Mode + * [00:00] W1T | Biphasic Counter Enable + */ + +#define PWMV4_REG_BIPHASIC_CNT_CTRL1 0x044 +/* + * BIPHASIC_CNT_CTRL1 Register Description + * [31:16] WO | Write Enable Mask for the lower half of the register + * Set bit `n` here to 1 if you wish to modify bit `n >> 16`= in + * the same write operation + * [15:11] RO | Reserved + * [10:04] RW | Biphasic Counter Filter Window Number + * [03:01] RO | Reserved + * [00:00] RW | Biphasic Counter Filter Enable + */ + +#define PWMV4_REG_BIPHASIC_CNT_TIMER 0x048 +/* + * BIPHASIC_CNT_TIMER Register Description + * [31:00] RW | Biphasic Counter Timer Value, in number of biphasic count= er + * timer clock cycles + */ + +#define PWMV4_REG_BIPHASIC_CNT_RES 0x04C +/* + * BIPHASIC_CNT_RES Register Description + * [31:00] RO | Biphasic Counter Result Value + * Constraints: Can only be read after INTSTS[9] is asserted + */ + +#define PWMV4_REG_BIPHASIC_CNT_RES_S 0x050 +/* + * BIPHASIC_CNT_RES_S Register Description + * [31:00] RO | Biphasic Counter Result Value with synchronised processing + * Can be read in real-time if BIPHASIC_CNT_CTRL0[7] was set= to 1 + */ + +#define PWMV4_REG_INTSTS 0x070 +/* + * INTSTS Register Description + * [31:10] RO | Reserved + * [09:09] W1C | Biphasic Counter Interrupt Status, 1 =3D interrupt assert= ed + * [08:08] W1C | Waveform Middle Interrupt Status, 1 =3D interrupt asserted + * [07:07] W1C | Waveform Max Interrupt Status, 1 =3D interrupt asserted + * [06:06] W1C | IR Transmission End Interrupt Status, 1 =3D interrupt ass= erted + * [05:05] W1C | Power Key Match Interrupt Status, 1 =3D interrupt asserted + * [04:04] W1C | Frequency Meter Interrupt Status, 1 =3D interrupt asserted + * [03:03] W1C | Reload Interrupt Status, 1 =3D interrupt asserted + * [02:02] W1C | Oneshot End Interrupt Status, 1 =3D interrupt asserted + * [01:01] W1C | HPC Capture Interrupt Status, 1 =3D interrupt asserted + * [00:00] W1C | LPC Capture Interrupt Status, 1 =3D interrupt asserted + */ +#define PWMV4_INT_LPC BIT(0) +#define PWMV4_INT_HPC BIT(1) +#define PWMV4_INT_LPC_W(v) FIELD_PREP_WM16(PWMV4_INT_LPC, \ + ((v) ? 1 : 0)) +#define PWMV4_INT_HPC_W(v) FIELD_PREP_WM16(PWMV4_INT_HPC, \ + ((v) ? 1 : 0)) + +#define PWMV4_REG_INT_EN 0x074 +/* + * INT_EN Register Description + * [31:16] WO | Write Enable Mask for the lower half of the register + * Set bit `n` here to 1 if you wish to modify bit `n >> 16`= in + * the same write operation + * [15:10] RO | Reserved + * [09:09] RW | Biphasic Counter Interrupt Enable, 1 =3D enabled + * [08:08] W1C | Waveform Middle Interrupt Enable, 1 =3D enabled + * [07:07] W1C | Waveform Max Interrupt Enable, 1 =3D enabled + * [06:06] W1C | IR Transmission End Interrupt Enable, 1 =3D enabled + * [05:05] W1C | Power Key Match Interrupt Enable, 1 =3D enabled + * [04:04] W1C | Frequency Meter Interrupt Enable, 1 =3D enabled + * [03:03] W1C | Reload Interrupt Enable, 1 =3D enabled + * [02:02] W1C | Oneshot End Interrupt Enable, 1 =3D enabled + * [01:01] W1C | HPC Capture Interrupt Enable, 1 =3D enabled + * [00:00] W1C | LPC Capture Interrupt Enable, 1 =3D enabled + */ + +#define PWMV4_REG_INT_MASK 0x078 +/* + * INT_MASK Register Description + * [31:16] WO | Write Enable Mask for the lower half of the register + * Set bit `n` here to 1 if you wish to modify bit `n >> 16`= in + * the same write operation + * [15:10] RO | Reserved + * [09:09] RW | Biphasic Counter Interrupt Masked, 1 =3D masked + * [08:08] W1C | Waveform Middle Interrupt Masked, 1 =3D masked + * [07:07] W1C | Waveform Max Interrupt Masked, 1 =3D masked + * [06:06] W1C | IR Transmission End Interrupt Masked, 1 =3D masked + * [05:05] W1C | Power Key Match Interrupt Masked, 1 =3D masked + * [04:04] W1C | Frequency Meter Interrupt Masked, 1 =3D masked + * [03:03] W1C | Reload Interrupt Masked, 1 =3D masked + * [02:02] W1C | Oneshot End Interrupt Masked, 1 =3D masked + * [01:01] W1C | HPC Capture Interrupt Masked, 1 =3D masked + * [00:00] W1C | LPC Capture Interrupt Masked, 1 =3D masked + */ + +static inline u32 mfpwm_reg_read(void __iomem *base, u32 reg) +{ + return readl(base + reg); +} + +static inline void mfpwm_reg_write(void __iomem *base, u32 reg, u32 val) +{ + writel(val, base + reg); +} + +/** + * mfpwm_acquire - try becoming the active mfpwm function device + * @pwmf: pointer to the calling driver instance's &struct rockchip_mfpwm_= func + * + * mfpwm device "function" drivers must call this function before doing an= ything + * that either modifies or relies on the parent device's state, such as cl= ocks, + * enabling/disabling outputs, modifying shared regs etc. + * + * The return statues should always be checked. + * + * All mfpwm_acquire() calls must be balanced with corresponding mfpwm_rel= ease() + * calls once the device is no longer making changes that affect other dev= ices, + * or stops producing user-visible effects that depend on the current devi= ce + * state being kept as-is. (e.g. after the PWM output signal is stopped) + * + * The same device function may mfpwm_acquire() multiple times while it al= ready + * is active, i.e. it is re-entrant, though it needs to balance this with = the + * same number of mfpwm_release() calls. + * + * Context: This function does not sleep. + * + * Return: + * * %0 - success + * * %-EBUSY - a different device function is active + * * %-EOVERFLOW - the acquire counter is at its maximum + */ +extern int __must_check mfpwm_acquire(struct rockchip_mfpwm_func *pwmf); + +/** + * mfpwm_release - drop usage of active mfpwm device function by 1 + * @pwmf: pointer to the calling driver instance's &struct rockchip_mfpwm_= func + * + * This is the balancing call to mfpwm_acquire(). If no users of the device + * function remain, set the mfpwm device to have no active device function, + * allowing other device functions to claim it. + */ +extern void mfpwm_release(const struct rockchip_mfpwm_func *pwmf); + +#endif /* __SOC_ROCKCHIP_MFPWM_H__ */ --=20 2.51.1 From nobody Thu Dec 18 14:12:56 2025 Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 01EF53176ED; Mon, 27 Oct 2025 17:14:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.112 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761585282; cv=pass; b=Spvgn0YEBnbAeg5pya4Sp359nR4grhdtRPrRoGNic5T2IATJSRxczG8SUw6Qv4yvSkMre2PHMdsGO1aERgpT8lPPagAgfDK/NugTxCWTdtJDzcSk6SFxhIPWxOSg/cdCKCY3L+o1eUFWL9CQrwdkAvWZORiFaFZKySw5UXrMsJA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761585282; c=relaxed/simple; bh=PFhlVi8LhCU4b7Lto2h4SBuaW+IvZK+s6RzSim/2D/k=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ksWhBOHaf/4Jny6VkkxwmaOdtUJ+ebnU4yPrzNgGjL7zFlf41hgn39lpubaYcUkkslK07hnaSQ8SQw0VhT2WSpND4zRsmIRJ/D3/JxuIut2gzqGlva3FlbaKDr9LrcHT1ufG8ppAA/7q2TI7MhivNJSPi7jxvqPI9dsl2oDKFjY= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=nicolas.frattaroli@collabora.com header.b=I/gPFy/6; arc=pass smtp.client-ip=136.143.188.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=nicolas.frattaroli@collabora.com header.b="I/gPFy/6" ARC-Seal: i=1; a=rsa-sha256; t=1761585253; cv=none; d=zohomail.com; s=zohoarc; b=Aq+pwO21myOfwzDZrAXFA5IQcKT1CrD4xNE4MXeXu1KSfLAVxu/ihjLZOz478AKuf2FZV10tQrfIWaylCPfgS9nK1KdmzpnuIDwDvR7FleOG93mmpQlsbH3eZXjE6F1LsyNPeMEwIs1eJ1YmHE3rPPeBzs+fk7r+K5cVV2Aps3E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761585253; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=2D1kUd2uDKO2TIKsH+hL3dY2umEnQqrp35ZApho/dag=; b=E6A839B9A9xi5ciwvcbFjQifmoO55h37CbaLsy3qK0XKN525C3TEn3VtUGn308EOPcgjcyt40u7Zd7fXZeP/ZwHXEG6vMyt8Nc1GS7wHJf3MIyZi5fb+2NQLhtw6Zt4PhJEX3/kO7Tfd18BmDZqW/B4Ewp7ja5TM+q/RVmnkSUw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=nicolas.frattaroli@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1761585253; s=zohomail; d=collabora.com; i=nicolas.frattaroli@collabora.com; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=2D1kUd2uDKO2TIKsH+hL3dY2umEnQqrp35ZApho/dag=; b=I/gPFy/672lwPTh68vydpeaCVCdKr4GgmcwaHnGHXsPXByXcPQvjC4E4vqN9ZOt5 fijyXY6JP5la/OYjJ/aW5rgbl3PnUBv0I+en9NvBlMnRue2tAWOLR5tyb6XcWnD1sDc PBR5/uDTuW8WBTw/xJQH4K/cx5LQ4dmax6fuivWc= Received: by mx.zohomail.com with SMTPS id 1761585251562196.01283614677175; Mon, 27 Oct 2025 10:14:11 -0700 (PDT) From: Nicolas Frattaroli Date: Mon, 27 Oct 2025 18:11:58 +0100 Subject: [PATCH v3 3/5] pwm: Add rockchip PWMv4 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: <20251027-rk3576-pwm-v3-3-654a5cb1e3f8@collabora.com> References: <20251027-rk3576-pwm-v3-0-654a5cb1e3f8@collabora.com> In-Reply-To: <20251027-rk3576-pwm-v3-0-654a5cb1e3f8@collabora.com> To: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Heiko Stuebner , Lee Jones , William Breathitt Gray Cc: kernel@collabora.com, Jonas Karlman , Alexey Charkov , linux-rockchip@lists.infradead.org, linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Nicolas Frattaroli X-Mailer: b4 0.14.3 The Rockchip RK3576 brings with it a new PWM IP, in downstream code referred to as "v4". This new IP is different enough from the previous Rockchip IP that I felt it necessary to add a new driver for it, instead of shoehorning it in the old one. Add this new driver, based on the PWM core's waveform APIs. Its platform device is registered by the parent mfpwm driver, from which it also receives a little platform data struct, so that mfpwm can guarantee that all the platform device drivers spread across different subsystems for this specific hardware IP do not interfere with each other. Signed-off-by: Nicolas Frattaroli Tested-by: Damon Ding --- MAINTAINERS | 1 + drivers/pwm/Kconfig | 13 ++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-rockchip-v4.c | 353 ++++++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 368 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 8f3235ba825e..2079c2d51d5c 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -22372,6 +22372,7 @@ L: linux-rockchip@lists.infradead.org L: linux-pwm@vger.kernel.org S: Maintained F: Documentation/devicetree/bindings/pwm/rockchip,rk3576-pwm.yaml +F: drivers/pwm/pwm-rockchip-v4.c F: drivers/soc/rockchip/mfpwm.c F: include/soc/rockchip/mfpwm.h =20 diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig index c2fd3f4b62d9..b852a7b2a29d 100644 --- a/drivers/pwm/Kconfig +++ b/drivers/pwm/Kconfig @@ -615,6 +615,19 @@ config PWM_ROCKCHIP Generic PWM framework driver for the PWM controller found on Rockchip SoCs. =20 +config PWM_ROCKCHIP_V4 + tristate "Rockchip PWM v4 support" + depends on ARCH_ROCKCHIP || COMPILE_TEST + depends on HAS_IOMEM + depends on MFD_ROCKCHIP_MFPWM + help + Generic PWM framework driver for the PWM controller found on + later Rockchip SoCs such as the RK3576. + + Uses the Rockchip Multi-function PWM controller driver infrastructure + to guarantee fearlessly concurrent operation with other functions of + the same device implemented by drivers in other subsystems. + config PWM_SAMSUNG tristate "Samsung PWM support" depends on PLAT_SAMSUNG || ARCH_S5PV210 || ARCH_EXYNOS || COMPILE_TEST diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile index dfa8b4966ee1..fe0d16558d99 100644 --- a/drivers/pwm/Makefile +++ b/drivers/pwm/Makefile @@ -55,6 +55,7 @@ obj-$(CONFIG_PWM_RENESAS_RZG2L_GPT) +=3D pwm-rzg2l-gpt.o obj-$(CONFIG_PWM_RENESAS_RZ_MTU3) +=3D pwm-rz-mtu3.o obj-$(CONFIG_PWM_RENESAS_TPU) +=3D pwm-renesas-tpu.o obj-$(CONFIG_PWM_ROCKCHIP) +=3D pwm-rockchip.o +obj-$(CONFIG_PWM_ROCKCHIP_V4) +=3D pwm-rockchip-v4.o obj-$(CONFIG_PWM_SAMSUNG) +=3D pwm-samsung.o obj-$(CONFIG_PWM_SIFIVE) +=3D pwm-sifive.o obj-$(CONFIG_PWM_SL28CPLD) +=3D pwm-sl28cpld.o diff --git a/drivers/pwm/pwm-rockchip-v4.c b/drivers/pwm/pwm-rockchip-v4.c new file mode 100644 index 000000000000..7afa83f12b6a --- /dev/null +++ b/drivers/pwm/pwm-rockchip-v4.c @@ -0,0 +1,353 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2025 Collabora Ltd. + * + * A Pulse-Width-Modulation (PWM) generator driver for the generators foun= d in + * Rockchip SoCs such as the RK3576, internally referred to as "PWM v4". U= ses + * the MFPWM infrastructure to guarantee exclusive use over the device wit= hout + * other functions of the device from different drivers interfering with i= ts + * operation while it's active. + * + * Technical Reference Manual: Chapter 31 of the RK3506 TRM Part 1, a SoC = which + * uses the same PWM hardware and has a publicly available TRM. + * https://opensource.rock-chips.com/images/3/36/Rockchip_RK3506_TRM_Part_= 1_V1.2-20250811.pdf + * + * Authors: + * Nicolas Frattaroli + * + * Limitations: + * - When the output is disabled, it will end abruptly without letting the + * current period complete. + * TODO: This can be fixed in the driver in the future by having the ena= ble- + * to-disable transition switch to oneshot mode with one repetitio= n, + * and then disable the pwmclk and release mfpwm when the oneshot + * complete interrupt fires. + * - When the output is disabled, the pin will remain driven to whatever s= tate + * it last had. + * - Adjustments to the duty cycle will only take effect during the next p= eriod. + * - Adjustments to the period length will only take effect during the next + * period. + * - offset should be between 0 and (period - duty) + */ + +#include +#include +#include +#include + +struct rockchip_pwm_v4 { + struct rockchip_mfpwm_func *pwmf; + struct pwm_chip chip; +}; + +struct rockchip_pwm_v4_wf { + u32 period; + u32 duty; + u32 offset; + u8 enable; +}; + +static inline struct rockchip_pwm_v4 *to_rockchip_pwm_v4(struct pwm_chip *= chip) +{ + return pwmchip_get_drvdata(chip); +} + +/** + * rockchip_pwm_v4_round_single - convert a PWM parameter to hardware + * @rate: clock rate of the PWM clock, as per clk_get_rate + * @in_val: parameter in nanoseconds to convert + * + * Returns the rounded value, saturating at U32_MAX if too large + */ +static u32 rockchip_pwm_v4_round_single(unsigned long rate, u64 in_val) +{ + u64 tmp; + + tmp =3D mul_u64_u64_div_u64(rate, in_val, NSEC_PER_SEC); + if (tmp > U32_MAX) + tmp =3D U32_MAX; + + return (u32)tmp; +} + +/** + * rockchip_pwm_v4_round_params - convert PWM parameters to hardware + * @rate: PWM clock rate to do the calculations at + * @duty: PWM duty cycle in nanoseconds + * @period: PWM period in nanoseconds + * @offset: PWM offset in nanoseconds + * @out_duty: pointer to where the rounded duty value should be stored + * @out_period: pointer to where the rounded period value should be stored + * @out_offset: pointer to where the rounded offset value should be stored + * + * Convert nanosecond-based duty/period/offset parameters to the PWM hardw= are's + * native rounded representation in number of cycles at clock rate @rate. = Should + * any of the input parameters be out of range for the hardware, the + * corresponding output parameter is the maximum permissible value for said + * parameter with considerations to the others. + */ +static void rockchip_pwm_v4_round_params(unsigned long rate, u64 duty, + u64 period, u64 offset, u32 *out_duty, + u32 *out_period, u32 *out_offset) +{ + *out_period =3D rockchip_pwm_v4_round_single(rate, period); + + *out_duty =3D rockchip_pwm_v4_round_single(rate, duty); + + /* As per TRM, PWM_OFFSET: "The value ranges from 0 to (period-duty)" */ + *out_offset =3D rockchip_pwm_v4_round_single(rate, offset); + if (*out_offset > (*out_period - *out_duty)) + *out_offset =3D *out_period - *out_duty; +} + +static int rockchip_pwm_v4_round_wf_tohw(struct pwm_chip *chip, + struct pwm_device *pwm, + const struct pwm_waveform *wf, + void *_wfhw) +{ + struct rockchip_pwm_v4 *pc =3D to_rockchip_pwm_v4(chip); + struct rockchip_pwm_v4_wf *wfhw =3D _wfhw; + unsigned long rate; + + rate =3D clk_get_rate(pc->pwmf->core); + + rockchip_pwm_v4_round_params(rate, wf->duty_length_ns, + wf->period_length_ns, wf->duty_offset_ns, + &wfhw->duty, &wfhw->period, &wfhw->offset); + + if (wf->period_length_ns > 0) + wfhw->enable =3D PWMV4_EN_BOTH_MASK; + else + wfhw->enable =3D 0; + + dev_dbg(&chip->dev, + "tohw: duty %llu -> %u, period %llu -> %u, offset %llu -> %u, rate %lu\n= ", + wf->duty_length_ns, wfhw->duty, wf->period_length_ns, + wfhw->period, wf->duty_offset_ns, wfhw->offset, rate); + + return 0; +} + +static int rockchip_pwm_v4_round_wf_fromhw(struct pwm_chip *chip, + struct pwm_device *pwm, + const void *_wfhw, + struct pwm_waveform *wf) +{ + struct rockchip_pwm_v4 *pc =3D to_rockchip_pwm_v4(chip); + const struct rockchip_pwm_v4_wf *wfhw =3D _wfhw; + unsigned long rate; + + rate =3D clk_get_rate(pc->pwmf->core); + + if (rockchip_pwm_v4_is_enabled(wfhw->enable)) { + if (!rate) + return -EINVAL; + + wf->period_length_ns =3D (u64)wfhw->period * NSEC_PER_SEC / rate; + wf->duty_length_ns =3D (u64)wfhw->duty * NSEC_PER_SEC / rate; + wf->duty_offset_ns =3D (u64)wfhw->offset * NSEC_PER_SEC / rate; + } else { + wf->period_length_ns =3D 0; + wf->duty_length_ns =3D 0; + wf->duty_offset_ns =3D 0; + } + + dev_dbg(&chip->dev, + "fromhw: duty %u -> %llu, period %u -> %llu, offset %u -> %llu, rate %lu= \n", + wfhw->duty, wf->duty_length_ns, wfhw->period, + wf->period_length_ns, wfhw->offset, wf->duty_offset_ns, rate); + + return 0; +} + +static int rockchip_pwm_v4_read_wf(struct pwm_chip *chip, struct pwm_devic= e *pwm, + void *_wfhw) +{ + struct rockchip_pwm_v4 *pc =3D to_rockchip_pwm_v4(chip); + struct rockchip_pwm_v4_wf *wfhw =3D _wfhw; + int ret =3D 0; + + + ret =3D mfpwm_acquire(pc->pwmf); + if (ret) + return ret; + + wfhw->period =3D mfpwm_reg_read(pc->pwmf->base, PWMV4_REG_PERIOD); + wfhw->duty =3D mfpwm_reg_read(pc->pwmf->base, PWMV4_REG_DUTY); + wfhw->offset =3D mfpwm_reg_read(pc->pwmf->base, PWMV4_REG_OFFSET); + wfhw->enable =3D mfpwm_reg_read(pc->pwmf->base, PWMV4_REG_ENABLE) & PWMV4= _EN_BOTH_MASK; + + mfpwm_release(pc->pwmf); + + return 0; +} + +static int rockchip_pwm_v4_write_wf(struct pwm_chip *chip, struct pwm_devi= ce *pwm, + const void *_wfhw) +{ + struct rockchip_pwm_v4 *pc =3D to_rockchip_pwm_v4(chip); + const struct rockchip_pwm_v4_wf *wfhw =3D _wfhw; + bool was_enabled =3D false; + int ret =3D 0; + + ret =3D mfpwm_acquire(pc->pwmf); + if (ret) + return ret; + + was_enabled =3D rockchip_pwm_v4_is_enabled(mfpwm_reg_read(pc->pwmf->base, + PWMV4_REG_ENABLE)); + + /* + * "But Nicolas", you ask with valid concerns, "why would you enable the + * PWM before setting all the parameter registers?" + * + * Excellent question, Mr. Reader M. Strawman! The RK3576 TRM Part 1 + * Section 34.6.3 specifies that this is the intended order of writes. + * Doing the PWM_EN and PWM_CLK_EN writes after the params but before + * the CTRL_UPDATE_EN, or even after the CTRL_UPDATE_EN, results in + * erratic behaviour where repeated turning on and off of the PWM may + * not turn it off under all circumstances. This is also why we don't + * use relaxed writes; it's not worth the footgun. + */ + mfpwm_reg_write(pc->pwmf->base, PWMV4_REG_ENABLE, + FIELD_PREP_WM16(PWMV4_EN_BOTH_MASK, wfhw->enable)); + + mfpwm_reg_write(pc->pwmf->base, PWMV4_REG_PERIOD, wfhw->period); + mfpwm_reg_write(pc->pwmf->base, PWMV4_REG_DUTY, wfhw->duty); + mfpwm_reg_write(pc->pwmf->base, PWMV4_REG_OFFSET, wfhw->offset); + + mfpwm_reg_write(pc->pwmf->base, PWMV4_REG_CTRL, PWMV4_CTRL_CONT_FLAGS); + + /* Commit new configuration to hardware output. */ + mfpwm_reg_write(pc->pwmf->base, PWMV4_REG_ENABLE, + PWMV4_CTRL_UPDATE_EN); + + if (rockchip_pwm_v4_is_enabled(wfhw->enable)) { + if (!was_enabled) { + dev_dbg(&chip->dev, "Enabling PWM output\n"); + ret =3D clk_enable(pc->pwmf->core); + if (ret) + goto err_mfpwm_release; + ret =3D clk_rate_exclusive_get(pc->pwmf->core); + if (ret) { + clk_disable(pc->pwmf->core); + goto err_mfpwm_release; + } + + /* + * Output should be on now, acquire device to guarantee + * exclusion with other device functions while it's on. + */ + ret =3D mfpwm_acquire(pc->pwmf); + if (ret) + goto err_mfpwm_release; + } + } else if (was_enabled) { + dev_dbg(&chip->dev, "Disabling PWM output\n"); + clk_rate_exclusive_put(pc->pwmf->core); + clk_disable(pc->pwmf->core); + /* Output is off now, extra release to balance extra acquire */ + mfpwm_release(pc->pwmf); + } + +err_mfpwm_release: + mfpwm_release(pc->pwmf); + + return ret; +} + +static const struct pwm_ops rockchip_pwm_v4_ops =3D { + .sizeof_wfhw =3D sizeof(struct rockchip_pwm_v4_wf), + .round_waveform_tohw =3D rockchip_pwm_v4_round_wf_tohw, + .round_waveform_fromhw =3D rockchip_pwm_v4_round_wf_fromhw, + .read_waveform =3D rockchip_pwm_v4_read_wf, + .write_waveform =3D rockchip_pwm_v4_write_wf, +}; + +static bool rockchip_pwm_v4_on_and_continuous(struct rockchip_pwm_v4 *pc) +{ + bool en; + u32 val; + + en =3D rockchip_pwm_v4_is_enabled(mfpwm_reg_read(pc->pwmf->base, + PWMV4_REG_ENABLE)); + val =3D mfpwm_reg_read(pc->pwmf->base, PWMV4_REG_CTRL); + + return en && ((val & PWMV4_MODE_MASK) =3D=3D PWMV4_MODE_CONT); +} + +static int rockchip_pwm_v4_probe(struct platform_device *pdev) +{ + struct rockchip_mfpwm_func *pwmf =3D dev_get_platdata(&pdev->dev); + struct rockchip_pwm_v4 *pc; + struct pwm_chip *chip; + struct device *dev =3D &pdev->dev; + int ret; + + /* + * For referencing the PWM in the DT to work, we need the parent MFD + * device's OF node. Code shamelessly adapted from `drivers/pci/of.c`'s + * pci_set_of_node(), which does this for bus reasons. + */ + dev->parent->of_node_reused =3D true; + device_set_node(dev, + of_fwnode_handle(no_free_ptr(dev->parent->of_node))); + + chip =3D devm_pwmchip_alloc(dev, 1, sizeof(*pc)); + if (IS_ERR(chip)) + return PTR_ERR(chip); + + pc =3D to_rockchip_pwm_v4(chip); + pc->pwmf =3D pwmf; + + ret =3D mfpwm_acquire(pwmf); + if (ret =3D=3D -EBUSY) + dev_warn(dev, "PWM hardware already in use, can't check initial state\n"= ); + else if (ret < 0) + return dev_err_probe(dev, ret, "Couldn't acquire mfpwm in probe\n"); + + if (!rockchip_pwm_v4_on_and_continuous(pc)) + mfpwm_release(pwmf); + else { + dev_dbg(dev, "PWM was already on at probe time\n"); + ret =3D clk_enable(pwmf->core); + if (ret) + return dev_err_probe(dev, ret, "Enabling pwm clock failed\n"); + ret =3D clk_rate_exclusive_get(pc->pwmf->core); + if (ret) { + clk_disable(pwmf->core); + return dev_err_probe(dev, ret, "Protecting pwm clock failed\n"); + } + } + + platform_set_drvdata(pdev, chip); + + chip->ops =3D &rockchip_pwm_v4_ops; + + ret =3D devm_pwmchip_add(dev, chip); + if (ret) + return dev_err_probe(dev, ret, "Failed to add PWM chip\n"); + + return 0; +} + +static const struct platform_device_id rockchip_pwm_v4_ids[] =3D { + { .name =3D "pwm-rockchip-v4", }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(platform, rockchip_pwm_v4_ids); + +static struct platform_driver rockchip_pwm_v4_driver =3D { + .probe =3D rockchip_pwm_v4_probe, + .driver =3D { + .name =3D "pwm-rockchip-v4", + }, + .id_table =3D rockchip_pwm_v4_ids, +}; +module_platform_driver(rockchip_pwm_v4_driver); + +MODULE_AUTHOR("Nicolas Frattaroli "); +MODULE_DESCRIPTION("Rockchip PWMv4 Driver"); +MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS("ROCKCHIP_MFPWM"); +MODULE_ALIAS("platform:pwm-rockchip-v4"); --=20 2.51.1 From nobody Thu Dec 18 14:12:56 2025 Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB07B315D32; Mon, 27 Oct 2025 17:14:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.112 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761585275; cv=pass; b=RMwvDAOtxbpNngy4+M4zPZaJ7r7lL+YpAE4NJisIN6g5qzptXz0LdWWTyEBzc7NLJ+34vNRfWWDeEY0YYCd37BA1noZ8Vz42a2MCZ3aiejzzMWekPpoP5LJCw2WZqj3dK2lrm5Ljc8JpZDygJfDglYCurthbZnSiZxjyl4uhD3Y= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761585275; c=relaxed/simple; bh=WkjpIaqOVm27hyM4GjElAXAgqb8FWaod5zZ5duuezec=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pyulwBVdmz+ayjUqXCNg0oINbbeK1rgpk6WRRo4vUiJwQ7W/8lySzOBR+9KRBR++vAKLBCNG4A+tXqmqQu+1BH0217mLLI2IES8UynXQLLvmyKrC86IzHmpSomZaIHH/6sqjnKb0OyUxXQrTPyjf1CEYoDSFmig3GTvrI7LQbhA= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=nicolas.frattaroli@collabora.com header.b=KQk5vLOc; arc=pass smtp.client-ip=136.143.188.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=nicolas.frattaroli@collabora.com header.b="KQk5vLOc" ARC-Seal: i=1; a=rsa-sha256; t=1761585257; cv=none; d=zohomail.com; s=zohoarc; b=D9iklI4wmWVXRCBdcbEKZqp8UagSHiUd1C0ddQt64HSh+OxUvRgpdzAQ7eFq7YR4xaBeSfP6in1AwmeyO/wP1TGR+YwkasyJKgmVeF0ZeZFSns/5z1msz0bT39KuW/Sm6XPMxmO5GwQz9a8oPVByTperW3v7rSch32CFV0/4PHA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761585257; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=BkZC/UH30pgFjHHpxrxGEL/0mUOglLr4vaPq6NDjtYU=; b=JD0YTCUD12zeVcq/gGjPqFnkxykAA0F7tswBkQA81MP8cBO/legZuhMtDW2kHuQmJkUAEGRYLtVfNil5Eer61RmgCtFVHfszPyDfBlkvwDc+ow74mel9hxM7Z+KEe1Luxw//zL5iDt4aF98E8gg5T3VbDV1+cCbReVC5H35xUo0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=nicolas.frattaroli@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1761585257; s=zohomail; d=collabora.com; i=nicolas.frattaroli@collabora.com; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=BkZC/UH30pgFjHHpxrxGEL/0mUOglLr4vaPq6NDjtYU=; b=KQk5vLOcRzEiNV7eI1kYRi3l4MQVzVVZ7N9rk6hMBS1CJscEf5MZZzCU1m5DL7Nb TAEkZWNu7NSAxCjAcZZdkYqb7q17hy4GrAsp3Dg16rOL9pKFXMH4hWqTJBgc2T2eNMa ONNurOajfcqlhDrXTS/KbfD3Afmj4ekkx/7b5+tw= Received: by mx.zohomail.com with SMTPS id 1761585255797123.46488322325456; Mon, 27 Oct 2025 10:14:15 -0700 (PDT) From: Nicolas Frattaroli Date: Mon, 27 Oct 2025 18:11:59 +0100 Subject: [PATCH v3 4/5] counter: Add rockchip-pwm-capture 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: <20251027-rk3576-pwm-v3-4-654a5cb1e3f8@collabora.com> References: <20251027-rk3576-pwm-v3-0-654a5cb1e3f8@collabora.com> In-Reply-To: <20251027-rk3576-pwm-v3-0-654a5cb1e3f8@collabora.com> To: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Heiko Stuebner , Lee Jones , William Breathitt Gray Cc: kernel@collabora.com, Jonas Karlman , Alexey Charkov , linux-rockchip@lists.infradead.org, linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Nicolas Frattaroli X-Mailer: b4 0.14.3 Among many other things, Rockchip's new PWMv4 IP in the RK3576 supports PWM capture functionality. Add a basic driver for this that works to expose HPC/LPC counts and state change events to userspace through the counter framework. It's quite basic, but works well enough to demonstrate the device function exclusion stuff that mfpwm does, in order to eventually support all the functions of this device in drivers within their appropriate subsystems, without them interfering with each other. Signed-off-by: Nicolas Frattaroli --- MAINTAINERS | 1 + drivers/counter/Kconfig | 13 ++ drivers/counter/Makefile | 1 + drivers/counter/rockchip-pwm-capture.c | 306 +++++++++++++++++++++++++++++= ++++ 4 files changed, 321 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 2079c2d51d5c..17d625ef76d1 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -22372,6 +22372,7 @@ L: linux-rockchip@lists.infradead.org L: linux-pwm@vger.kernel.org S: Maintained F: Documentation/devicetree/bindings/pwm/rockchip,rk3576-pwm.yaml +F: drivers/counter/rockchip-pwm-capture.c F: drivers/pwm/pwm-rockchip-v4.c F: drivers/soc/rockchip/mfpwm.c F: include/soc/rockchip/mfpwm.h diff --git a/drivers/counter/Kconfig b/drivers/counter/Kconfig index d30d22dfe577..12e823dd0ec7 100644 --- a/drivers/counter/Kconfig +++ b/drivers/counter/Kconfig @@ -90,6 +90,19 @@ config MICROCHIP_TCB_CAPTURE To compile this driver as a module, choose M here: the module will be called microchip-tcb-capture. =20 +config ROCKCHIP_PWM_CAPTURE + tristate "Rockchip PWM Counter Capture driver" + depends on ARCH_ROCKCHIP || COMPILE_TEST + depends on MFD_ROCKCHIP_MFPWM + depends on HAS_IOMEM + help + Generic counter framework driver for the multi-function PWM on + Rockchip SoCs such as the RK3576. + + Uses the Rockchip Multi-function PWM controller driver infrastructure + to guarantee exclusive operation with other functions of the same + device implemented by drivers in other subsystems. + config RZ_MTU3_CNT tristate "Renesas RZ/G2L MTU3a counter driver" depends on RZ_MTU3 diff --git a/drivers/counter/Makefile b/drivers/counter/Makefile index fa3c1d08f706..2bfcfc2c584b 100644 --- a/drivers/counter/Makefile +++ b/drivers/counter/Makefile @@ -17,3 +17,4 @@ obj-$(CONFIG_FTM_QUADDEC) +=3D ftm-quaddec.o obj-$(CONFIG_MICROCHIP_TCB_CAPTURE) +=3D microchip-tcb-capture.o obj-$(CONFIG_INTEL_QEP) +=3D intel-qep.o obj-$(CONFIG_TI_ECAP_CAPTURE) +=3D ti-ecap-capture.o +obj-$(CONFIG_ROCKCHIP_PWM_CAPTURE) +=3D rockchip-pwm-capture.o diff --git a/drivers/counter/rockchip-pwm-capture.c b/drivers/counter/rockc= hip-pwm-capture.c new file mode 100644 index 000000000000..63e1286303f9 --- /dev/null +++ b/drivers/counter/rockchip-pwm-capture.c @@ -0,0 +1,306 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2025 Collabora Ltd. + * + * A counter driver for the Pulse-Width-Modulation (PWM) hardware found on + * Rockchip SoCs such as the RK3576, internally referred to as "PWM v4". It + * allows for measuring the high cycles and low cycles of a PWM signal thr= ough + * the generic counter framework, while guaranteeing exclusive use over the + * MFPWM device while the counter is enabled. + * + * Authors: + * Nicolas Frattaroli + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define RKPWMC_INT_MASK (PWMV4_INT_LPC | PWMV4_INT_HPC) + +struct rockchip_pwm_capture { + struct rockchip_mfpwm_func *pwmf; + struct counter_device *counter; + bool is_enabled; + spinlock_t enable_lock; +}; + +/* + * Channel 0 receives a state changed notification whenever the LPC interr= upt + * fires. + * + * Channel 1 receives a state changed notification whenever the HPC interr= upt + * fires. + */ +static struct counter_signal rkpwmc_signals[] =3D { + { + .id =3D 0, + .name =3D "Channel 0" + }, + { + .id =3D 1, + .name =3D "Channel 1" + }, +}; + +static const enum counter_synapse_action rkpwmc_hpc_lpc_actions[] =3D { + COUNTER_SYNAPSE_ACTION_BOTH_EDGES, + +}; + +static struct counter_synapse rkpwmc_pwm_synapses[] =3D { + { + .actions_list =3D rkpwmc_hpc_lpc_actions, + .num_actions =3D ARRAY_SIZE(rkpwmc_hpc_lpc_actions), + .signal =3D &rkpwmc_signals[0] + }, + { + .actions_list =3D rkpwmc_hpc_lpc_actions, + .num_actions =3D ARRAY_SIZE(rkpwmc_hpc_lpc_actions), + .signal =3D &rkpwmc_signals[1] + }, +}; + +static const enum counter_function rkpwmc_functions[] =3D { + COUNTER_FUNCTION_INCREASE, +}; + +static int rkpwmc_enable_read(struct counter_device *counter, + struct counter_count *count, + u8 *enable) +{ + struct rockchip_pwm_capture *pc =3D counter_priv(counter); + + guard(spinlock)(&pc->enable_lock); + + *enable =3D pc->is_enabled; + + return 0; +} + +static int rkpwmc_enable_write(struct counter_device *counter, + struct counter_count *count, + u8 enable) +{ + struct rockchip_pwm_capture *pc =3D counter_priv(counter); + int ret; + + guard(spinlock)(&pc->enable_lock); + + if (!!enable !=3D pc->is_enabled) { + ret =3D mfpwm_acquire(pc->pwmf); + if (ret) + return ret; + + if (enable) { + mfpwm_reg_write(pc->pwmf->base, PWMV4_REG_ENABLE, + PWMV4_EN(false)); + mfpwm_reg_write(pc->pwmf->base, PWMV4_REG_CTRL, + PWMV4_CTRL_CAP_FLAGS); + mfpwm_reg_write(pc->pwmf->base, PWMV4_REG_INT_EN, + PWMV4_INT_LPC_W(true) | + PWMV4_INT_HPC_W(true)); + mfpwm_reg_write(pc->pwmf->base, PWMV4_REG_ENABLE, + PWMV4_EN(true) | PWMV4_CLK_EN(true)); + + ret =3D clk_enable(pc->pwmf->core); + if (ret) + goto err_release; + + ret =3D clk_rate_exclusive_get(pc->pwmf->core); + if (ret) + goto err_disable_pwm_clk; + + ret =3D mfpwm_acquire(pc->pwmf); + if (ret) + goto err_unprotect_pwm_clk; + + pc->is_enabled =3D true; + } else { + mfpwm_reg_write(pc->pwmf->base, PWMV4_REG_INT_EN, + PWMV4_INT_LPC_W(false) | + PWMV4_INT_HPC_W(false)); + mfpwm_reg_write(pc->pwmf->base, PWMV4_REG_ENABLE, + PWMV4_EN(false) | PWMV4_CLK_EN(false)); + clk_rate_exclusive_put(pc->pwmf->core); + clk_disable(pc->pwmf->core); + pc->is_enabled =3D false; + mfpwm_release(pc->pwmf); + } + + mfpwm_release(pc->pwmf); + } + + return 0; + +err_unprotect_pwm_clk: + clk_rate_exclusive_put(pc->pwmf->core); +err_disable_pwm_clk: + clk_disable(pc->pwmf->core); +err_release: + mfpwm_release(pc->pwmf); + + return ret; +} + +static struct counter_comp rkpwmc_ext[] =3D { + COUNTER_COMP_ENABLE(rkpwmc_enable_read, rkpwmc_enable_write), +}; + +enum rkpwmc_count_id { + COUNT_LPC =3D 0, + COUNT_HPC =3D 1, +}; + +static struct counter_count rkpwmc_counts[] =3D { + { + .id =3D COUNT_LPC, + .name =3D "PWM core clock cycles during which the signal was low", + .functions_list =3D rkpwmc_functions, + .num_functions =3D ARRAY_SIZE(rkpwmc_functions), + .synapses =3D rkpwmc_pwm_synapses, + .num_synapses =3D ARRAY_SIZE(rkpwmc_pwm_synapses), + .ext =3D rkpwmc_ext, + .num_ext =3D ARRAY_SIZE(rkpwmc_ext), + }, + { + .id =3D COUNT_HPC, + .name =3D "PWM core clock cycles during which the signal was high", + .functions_list =3D rkpwmc_functions, + .num_functions =3D ARRAY_SIZE(rkpwmc_functions), + .synapses =3D rkpwmc_pwm_synapses, + .num_synapses =3D ARRAY_SIZE(rkpwmc_pwm_synapses), + .ext =3D rkpwmc_ext, + .num_ext =3D ARRAY_SIZE(rkpwmc_ext), + }, +}; + +static int rkpwmc_count_read(struct counter_device *counter, + struct counter_count *count, u64 *value) +{ + struct rockchip_pwm_capture *pc =3D counter_priv(counter); + + guard(spinlock)(&pc->enable_lock); + + if (!pc->is_enabled) { + *value =3D 0; + return 0; + } + + switch (count->id) { + case COUNT_LPC: + *value =3D mfpwm_reg_read(pc->pwmf->base, PWMV4_REG_LPC); + return 0; + case COUNT_HPC: + *value =3D mfpwm_reg_read(pc->pwmf->base, PWMV4_REG_HPC); + return 0; + default: + return -EINVAL; + } +} + +static const struct counter_ops rkpwmc_ops =3D { + .count_read =3D rkpwmc_count_read, +}; + +static irqreturn_t rkpwmc_irq_handler(int irq, void *data) +{ + struct rockchip_pwm_capture *pc =3D data; + u32 intsts; + u32 clr =3D 0; + + intsts =3D mfpwm_reg_read(pc->pwmf->base, PWMV4_REG_INTSTS); + + if (!(intsts & RKPWMC_INT_MASK)) + return IRQ_NONE; + + if (intsts & PWMV4_INT_LPC) { + clr |=3D PWMV4_INT_LPC; + counter_push_event(pc->counter, COUNTER_EVENT_CHANGE_OF_STATE, 0); + } + + if (intsts & PWMV4_INT_HPC) { + clr |=3D PWMV4_INT_HPC; + counter_push_event(pc->counter, COUNTER_EVENT_CHANGE_OF_STATE, 1); + } + + if (clr) + mfpwm_reg_write(pc->pwmf->base, PWMV4_REG_INTSTS, clr); + + /* If other interrupt status bits are set, they're not for this driver */ + if (intsts !=3D clr) + return IRQ_NONE; + + return IRQ_HANDLED; +} + +static int rockchip_pwm_capture_probe(struct platform_device *pdev) +{ + struct rockchip_mfpwm_func *pwmf =3D dev_get_platdata(&pdev->dev); + struct rockchip_pwm_capture *pc; + struct counter_device *counter; + int ret; + + /* Set our (still unset) OF node to the parent MFD device's OF node */ + pdev->dev.parent->of_node_reused =3D true; + device_set_node(&pdev->dev, + of_fwnode_handle(no_free_ptr(pdev->dev.parent->of_node))); + + counter =3D devm_counter_alloc(&pdev->dev, sizeof(*pc)); + if (IS_ERR(counter)) + return PTR_ERR(counter); + + pc =3D counter_priv(counter); + pc->pwmf =3D pwmf; + spin_lock_init(&pc->enable_lock); + + platform_set_drvdata(pdev, pc); + + ret =3D devm_request_irq(&pdev->dev, pwmf->irq, rkpwmc_irq_handler, + IRQF_SHARED, pdev->name, pc); + if (ret) + return dev_err_probe(&pdev->dev, ret, "Failed requesting IRQ\n"); + + counter->name =3D pdev->name; + counter->signals =3D rkpwmc_signals; + counter->num_signals =3D ARRAY_SIZE(rkpwmc_signals); + counter->ops =3D &rkpwmc_ops; + counter->counts =3D rkpwmc_counts; + counter->num_counts =3D ARRAY_SIZE(rkpwmc_counts); + + pc->counter =3D counter; + + ret =3D devm_counter_add(&pdev->dev, counter); + if (ret < 0) + return dev_err_probe(&pdev->dev, ret, "Failed to add counter\n"); + + return 0; +} + +static const struct platform_device_id rockchip_pwm_capture_id_table[] =3D= { + { .name =3D "rockchip-pwm-capture", }, + { /* sentinel */ }, +}; +MODULE_DEVICE_TABLE(platform, rockchip_pwm_capture_id_table); + +static struct platform_driver rockchip_pwm_capture_driver =3D { + .probe =3D rockchip_pwm_capture_probe, + .id_table =3D rockchip_pwm_capture_id_table, + .driver =3D { + .name =3D "rockchip-pwm-capture", + }, +}; +module_platform_driver(rockchip_pwm_capture_driver); + +MODULE_AUTHOR("Nicolas Frattaroli "); +MODULE_DESCRIPTION("Rockchip PWM Counter Capture Driver"); +MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS("ROCKCHIP_MFPWM"); +MODULE_IMPORT_NS("COUNTER"); +MODULE_ALIAS("platform:rockchip-pwm-capture"); --=20 2.51.1 From nobody Thu Dec 18 14:12:56 2025 Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2705831812F; Mon, 27 Oct 2025 17:14:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.112 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761585285; cv=pass; b=Y6jOep7RWTwrUa7PhJHRjXtZyMMsSOUDUcZjH3J0IfETG8JZF1yf0gHn75qio5OD8hAcmINRUimZedd1VTZ25XJiuo5Ud7dU7/9Onh0CL9Lz6E0hSE/ZyAXWAk1bgSHXrRCaskrgj9ouUSmFcxnBiBiuFn272AfsXZA5y8T+gTA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761585285; c=relaxed/simple; bh=ix2u/aDF0qBqpRsbrrIdQr69LEu45/SyoViPDvvpB2I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Gpob1VYx2Rm4LSqQ7xyQkJh6XMtdJVcO3cCCmzMzN4gMqk+DhjCDaAESjsZW5LqLWdbeNeTBNPHpwyu2pB7ISH7go3XANF4nRiMTZFJi0Tc5iiMe2ppoR5/7X9w1zLnQ1i0qu1h7LDg5MKdGtGv9JZI06mTxHvH3S4KCI3gO6AI= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=nicolas.frattaroli@collabora.com header.b=L0ZNfKKI; arc=pass smtp.client-ip=136.143.188.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=nicolas.frattaroli@collabora.com header.b="L0ZNfKKI" ARC-Seal: i=1; a=rsa-sha256; t=1761585261; cv=none; d=zohomail.com; s=zohoarc; b=nzWjLn5Z4BUtKuZOvnAE43l/MLdQnAujCdOBInOotQGFGQrQvs27n3K+M22Z/Ff3tQVam79kLGiANlt9C60hjKo6j0flyw7Kuo9wKwRYpy8xq13UEwySAJNM4TbhfF9HLSmeLfX389ykt9dhHCgR/cfLlwHKJyrMpFvAraJNikM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761585261; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=I67bOjSOGdKubyE5dBnDfBCdL3Y5Tg26wF/RzeIjnlA=; b=J6+jlv9flXuV2f3w0ic3kEwtuPbBwMwPEHPHn/Q+uPp3OoV1n0eHkwR1sxtV2JzUfUvkgbw+/GpCQ8/HVykuWPUsgkvZOs5C3AZL0Eo9d7/BFPTjNVvn+cw9ZObwPzCH+aGwtiAdYPbTPw8OWq1LBXYGPkhS48cqolcgGrb3x6w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=nicolas.frattaroli@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1761585261; s=zohomail; d=collabora.com; i=nicolas.frattaroli@collabora.com; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=I67bOjSOGdKubyE5dBnDfBCdL3Y5Tg26wF/RzeIjnlA=; b=L0ZNfKKIYVQySWQBrC0n0WMpXErySYf3/Y0qttS3k1ACADSfZMX84UzyDdnWjJW/ C0XMlxH2WjzLDdpzUt43wenuRRXqNaZ+l0+S5Of3V8g3J2bAyknVa81Lh3slRCNmFvR lxi/ndBhtVywtdHK/pFswg1gpaJlDBBvnu7BIn7w= Received: by mx.zohomail.com with SMTPS id 1761585260004677.38213585215; Mon, 27 Oct 2025 10:14:20 -0700 (PDT) From: Nicolas Frattaroli Date: Mon, 27 Oct 2025 18:12:00 +0100 Subject: [PATCH v3 5/5] arm64: dts: rockchip: add PWM nodes to RK3576 SoC dtsi 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: <20251027-rk3576-pwm-v3-5-654a5cb1e3f8@collabora.com> References: <20251027-rk3576-pwm-v3-0-654a5cb1e3f8@collabora.com> In-Reply-To: <20251027-rk3576-pwm-v3-0-654a5cb1e3f8@collabora.com> To: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Heiko Stuebner , Lee Jones , William Breathitt Gray Cc: kernel@collabora.com, Jonas Karlman , Alexey Charkov , linux-rockchip@lists.infradead.org, linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Nicolas Frattaroli X-Mailer: b4 0.14.3 The RK3576 SoC features three distinct PWM controllers, with variable numbers of channels. Add each channel as a separate node to the SoC's device tree, as they don't really overlap in register ranges. Signed-off-by: Nicolas Frattaroli --- arch/arm64/boot/dts/rockchip/rk3576.dtsi | 208 +++++++++++++++++++++++++++= ++++ 1 file changed, 208 insertions(+) diff --git a/arch/arm64/boot/dts/rockchip/rk3576.dtsi b/arch/arm64/boot/dts= /rockchip/rk3576.dtsi index f0c3ab00a7f3..1e135cf09efc 100644 --- a/arch/arm64/boot/dts/rockchip/rk3576.dtsi +++ b/arch/arm64/boot/dts/rockchip/rk3576.dtsi @@ -1030,6 +1030,32 @@ uart1: serial@27310000 { status =3D "disabled"; }; =20 + pwm0_2ch_0: pwm@27330000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x27330000 0x0 0x1000>; + clocks =3D <&cru CLK_PMU1PWM>, <&cru PCLK_PMU1PWM>, + <&cru CLK_PMU1PWM_OSC>, <&cru CLK_PMU1PWM_RC>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm0m0_ch0>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + + pwm0_2ch_1: pwm@27331000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x27331000 0x0 0x1000>; + clocks =3D <&cru CLK_PMU1PWM>, <&cru PCLK_PMU1PWM>, + <&cru CLK_PMU1PWM_OSC>, <&cru CLK_PMU1PWM_RC>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm0m0_ch1>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + pmu: power-management@27380000 { compatible =3D "rockchip,rk3576-pmu", "syscon", "simple-mfd"; reg =3D <0x0 0x27380000 0x0 0x800>; @@ -2480,6 +2506,188 @@ uart9: serial@2adc0000 { status =3D "disabled"; }; =20 + pwm1_6ch_0: pwm@2add0000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x2add0000 0x0 0x1000>; + clocks =3D <&cru CLK_PWM1>, <&cru PCLK_PWM1>, + <&cru CLK_OSC_PWM1>, <&cru CLK_RC_PWM1>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm1m0_ch0>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + + pwm1_6ch_1: pwm@2add1000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x2add1000 0x0 0x1000>; + clocks =3D <&cru CLK_PWM1>, <&cru PCLK_PWM1>, + <&cru CLK_OSC_PWM1>, <&cru CLK_RC_PWM1>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm1m0_ch1>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + + pwm1_6ch_2: pwm@2add2000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x2add2000 0x0 0x1000>; + clocks =3D <&cru CLK_PWM1>, <&cru PCLK_PWM1>, + <&cru CLK_OSC_PWM1>, <&cru CLK_RC_PWM1>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm1m0_ch2>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + + pwm1_6ch_3: pwm@2add3000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x2add3000 0x0 0x1000>; + clocks =3D <&cru CLK_PWM1>, <&cru PCLK_PWM1>, + <&cru CLK_OSC_PWM1>, <&cru CLK_RC_PWM1>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm1m0_ch3>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + + pwm1_6ch_4: pwm@2add4000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x2add4000 0x0 0x1000>; + clocks =3D <&cru CLK_PWM1>, <&cru PCLK_PWM1>, + <&cru CLK_OSC_PWM1>, <&cru CLK_RC_PWM1>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm1m0_ch4>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + + pwm1_6ch_5: pwm@2add5000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x2add5000 0x0 0x1000>; + clocks =3D <&cru CLK_PWM1>, <&cru PCLK_PWM1>, + <&cru CLK_OSC_PWM1>, <&cru CLK_RC_PWM1>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm1m0_ch5>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + + pwm2_8ch_0: pwm@2ade0000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x2ade0000 0x0 0x1000>; + clocks =3D <&cru CLK_PWM2>, <&cru PCLK_PWM2>, + <&cru CLK_OSC_PWM2>, <&cru CLK_RC_PWM2>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm2m0_ch0>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + + pwm2_8ch_1: pwm@2ade1000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x2ade1000 0x0 0x1000>; + clocks =3D <&cru CLK_PWM2>, <&cru PCLK_PWM2>, + <&cru CLK_OSC_PWM2>, <&cru CLK_RC_PWM2>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm2m0_ch1>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + + pwm2_8ch_2: pwm@2ade2000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x2ade2000 0x0 0x1000>; + clocks =3D <&cru CLK_PWM2>, <&cru PCLK_PWM2>, + <&cru CLK_OSC_PWM2>, <&cru CLK_RC_PWM2>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm2m0_ch2>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + + pwm2_8ch_3: pwm@2ade3000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x2ade3000 0x0 0x1000>; + clocks =3D <&cru CLK_PWM2>, <&cru PCLK_PWM2>, + <&cru CLK_OSC_PWM2>, <&cru CLK_RC_PWM2>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm2m0_ch3>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + + pwm2_8ch_4: pwm@2ade4000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x2ade4000 0x0 0x1000>; + clocks =3D <&cru CLK_PWM2>, <&cru PCLK_PWM2>, + <&cru CLK_OSC_PWM2>, <&cru CLK_RC_PWM2>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm2m0_ch4>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + + pwm2_8ch_5: pwm@2ade5000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x2ade5000 0x0 0x1000>; + clocks =3D <&cru CLK_PWM2>, <&cru PCLK_PWM2>, + <&cru CLK_OSC_PWM2>, <&cru CLK_RC_PWM2>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm2m0_ch5>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + + pwm2_8ch_6: pwm@2ade6000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x2ade6000 0x0 0x1000>; + clocks =3D <&cru CLK_PWM2>, <&cru PCLK_PWM2>, + <&cru CLK_OSC_PWM2>, <&cru CLK_RC_PWM2>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm2m0_ch6>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + + pwm2_8ch_7: pwm@2ade7000 { + compatible =3D "rockchip,rk3576-pwm"; + reg =3D <0x0 0x2ade7000 0x0 0x1000>; + clocks =3D <&cru CLK_PWM2>, <&cru PCLK_PWM2>, + <&cru CLK_OSC_PWM2>, <&cru CLK_RC_PWM2>; + clock-names =3D "pwm", "pclk", "osc", "rc"; + interrupts =3D ; + pinctrl-names =3D "default"; + pinctrl-0 =3D <&pwm2m0_ch7>; + #pwm-cells =3D <3>; + status =3D "disabled"; + }; + saradc: adc@2ae00000 { compatible =3D "rockchip,rk3576-saradc", "rockchip,rk3588-saradc"; reg =3D <0x0 0x2ae00000 0x0 0x10000>; --=20 2.51.1