From nobody Sat Feb 7 08:45:07 2026 Received: from smtpout-02.galae.net (smtpout-02.galae.net [185.246.84.56]) (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 A803136999F; Fri, 23 Jan 2026 09:33:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.84.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769160837; cv=none; b=a3VlDusyvmkiWbqD7L1J9qdWCFHcZa3t7O/8sQ19go5duTffloVaQFagwnMTH/G7kOu48/DP9m0euwnwjQiC6Y3a33PA2Lt9+jvBZiD/XyHbQgZwzrJjSGq+cxrVO/kfLy7W1h2/vVPURhe097gyKs8n0tEMBSnpAbIpOWoKI+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769160837; c=relaxed/simple; bh=in40fZfBJur7b2gTli4i7mrCZHp27MkqzsX2uoHgeGg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rzytR+i8qzUc32tyIRRMqa4hjx2sp1bHhwQCG4WpX5PpSfKWKhX+j1UmvOXac/x6FG5HntLmLeU1eEb6XS9Dh+56QXo8/HRWmCmliOxVwrm/GDaBVX6OpPp4Ts66LoF8zMBNnhbuA0vGOburUFWMcqtowchT//wUNBGjfJ2OCPE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=bUVGLMgC; arc=none smtp.client-ip=185.246.84.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="bUVGLMgC" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-02.galae.net (Postfix) with ESMTPS id 0697F1A2AFF; Fri, 23 Jan 2026 09:33:47 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id CF3396070A; Fri, 23 Jan 2026 09:33:46 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 306B6119A879B; Fri, 23 Jan 2026 10:33:44 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1769160825; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=AumlAK3D+nBnNV49+1NREtHq21qZzWdFJkGV6lEmtAE=; b=bUVGLMgCg9aRioam0lpyysvJCDaFLPOQhcfrTllGrrS2jyDuwMQsvJt0DH1KGe21byYjyQ 41mr/zGLcvhgMk9BaH0tjgwJexMTkbGFsE9h5FbgUppqrr2C+pI7BhTNDqBVAswNn3sANl 15BCg7BQMN1ocXNcO/FTGVaPaCmm8dz53qKRXeDisqC6/8Xen0AISJG7L8F6+qVmo3vtbx hR7tZQMi/x9RFsadJ181ywUbVyPO70D76TnEuYASXgXL69s5WYrUQ0QNvqJaohq1p7JNMu HatsgjnC6FWSEKDH21MhK8x8m/hCTBp+MX7YqjKJaNgB+nl25oLFHe4qzRqDLw== From: Richard Genoud To: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Philipp Zabel Cc: Paul Kocialkowski , Thomas Petazzoni , linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org, Richard Genoud Subject: [PATCH v3 1/4] dt-bindings: pwm: allwinner: add h616 pwm compatible Date: Fri, 23 Jan 2026 10:33:19 +0100 Message-ID: <20260123093322.1327389-2-richard.genoud@bootlin.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260123093322.1327389-1-richard.genoud@bootlin.com> References: <20260123093322.1327389-1-richard.genoud@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Content-Type: text/plain; charset="utf-8" Allwinner H616 PWM block is quite different from the A10 or H6, but at the end, it uses the same clocks as the H6; so the sun4i pwm binding can be used. It has 6 channels than can generate PWM waveforms. If the bypass is enabled (one bypass per channel) the output is no more a PWM waveform, but a clock that can (and is) used as input for other devices, like the AC300 PHY. Signed-off-by: Richard Genoud Acked-by: Conor Dooley --- .../bindings/pwm/allwinner,sun4i-a10-pwm.yaml | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Documentation/devicetree/bindings/pwm/allwinner,sun4i-a10-pwm.= yaml b/Documentation/devicetree/bindings/pwm/allwinner,sun4i-a10-pwm.yaml index 1197858e431f..4f58110ec98f 100644 --- a/Documentation/devicetree/bindings/pwm/allwinner,sun4i-a10-pwm.yaml +++ b/Documentation/devicetree/bindings/pwm/allwinner,sun4i-a10-pwm.yaml @@ -14,6 +14,9 @@ properties: "#pwm-cells": const: 3 =20 + "#clock-cells": + const: 1 + compatible: oneOf: - const: allwinner,sun4i-a10-pwm @@ -36,6 +39,7 @@ properties: - const: allwinner,sun50i-h5-pwm - const: allwinner,sun5i-a13-pwm - const: allwinner,sun50i-h6-pwm + - const: allwinner,sun50i-h616-pwm =20 reg: maxItems: 1 @@ -62,7 +66,9 @@ allOf: properties: compatible: contains: - const: allwinner,sun50i-h6-pwm + enum: + - allwinner,sun50i-h6-pwm + - allwinner,sun50i-h616-pwm =20 then: properties: @@ -83,6 +89,17 @@ allOf: clocks: maxItems: 1 =20 + - if: + not: + properties: + compatible: + contains: + const: allwinner,sun50i-h616-pwm + + then: + properties: + "#clock-cells": false + required: - compatible - reg --=20 2.47.3 From nobody Sat Feb 7 08:45:07 2026 Received: from smtpout-02.galae.net (smtpout-02.galae.net [185.246.84.56]) (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 30C9E36AB51; Fri, 23 Jan 2026 09:33:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.84.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769160841; cv=none; b=PUvBTfRo109S5M3e3DtGW5WVkxi6am6VHRPMZEElsNJx3oZwssvHlji/Ty/64M6s1vdn1hDANg66sWhZ/JKa1ZjXZDQ/9q85jXn9mh3shVv6u0gMFB/eZktNmjUm35UWwdIEK1iKRlRY1W78mENxxrWhu5HHmPSu4r8VInZ8Xu8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769160841; c=relaxed/simple; bh=vxPKh3Uk5Qh6m1T/VBIK92nyYlhidEkaN288evuUAMw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aOGCo7zGk/+NDjFCPMclLdwoNXv9cvG4svCW7ZBgXVu+UvCIAqG1wHd9U2BtEwIwTqzSTjBKv53lJfnO2cZ2whOtGXAm+KkKNnQNbjAKtWkm4GzMNy5s3QtdU6obUzCNXADCWXQVN4qjpY3UzJuJc7qYwEyAVItmbs8I/zzd1Hs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=kdIdkr18; arc=none smtp.client-ip=185.246.84.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="kdIdkr18" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-02.galae.net (Postfix) with ESMTPS id 493481A2B01; Fri, 23 Jan 2026 09:33:49 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 1D6D76070A; Fri, 23 Jan 2026 09:33:49 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 047D8119A87A5; Fri, 23 Jan 2026 10:33:45 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1769160827; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=Ms8wAat6gQPZ9VhGuSZWYT7s1dznXqpu6acVedmFDCc=; b=kdIdkr18nkmA0vWWHCk81yNJdxL9MHNo3DpXFaXd6tuHSP0yEsrKDW5Erz5R7koAxKsUId OYVrQp3M9/mMA7v5WN7bEjDCmVIyh2awVwiQ3jnRaTOaVtyN1wVRsQsYu0pt2o8psZnDgl 6Up2MULHPRckE3EdU5F9bno5aC+1U264u/gA/gF4d7HGsjA3G4UqsAm5LECFAnElKRT4ZV qee4b3M+M1bH/TNVfRl5QE3YFwpbGNf/kEN4OB0z5odo8rWQXAxqW2a98b7ZjmFdXKNgUR WxjekwZbfPQqhEfuzw1Yl1+v4XGnpFDeNjoqR/jAsW2vjBMse5AQxnCkOiAZ8w== From: Richard Genoud To: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Philipp Zabel Cc: Paul Kocialkowski , Thomas Petazzoni , linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org, Richard Genoud Subject: [PATCH v3 2/4] pwm: sun50i: Add H616 PWM support Date: Fri, 23 Jan 2026 10:33:20 +0100 Message-ID: <20260123093322.1327389-3-richard.genoud@bootlin.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260123093322.1327389-1-richard.genoud@bootlin.com> References: <20260123093322.1327389-1-richard.genoud@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Content-Type: text/plain; charset="utf-8" Add driver for Allwinner H616 PWM controller, supporting up to 6 channels. Those channels output can be either a PWM signal output or a clock output, thanks to the bypass. The channels are paired (0/1, 2/3 and 4/5) and each pair has a prescaler/mux/gate. Moreover, each channel has its own prescaler and bypass. The clock provider part of this driver is needed not only because the H616 PWM controller provides also clocks when bypass is enabled, but really because pwm-clock isn't fit to handle all cases here. pwm-clock would work if the 100MHz clock is requested, but if a lower clock is requested (like 24MHz), it will request a 42ns period to the PWM driver which will happily serve, with the 100MHz clock as input a 25MHz frequency and a duty cycle adjustable in the range [0-4]/4, because that is a sane thing to do for a PWM. The information missing is that a real clock is resquested, not a PWM. Signed-off-by: Richard Genoud --- drivers/pwm/Kconfig | 12 + drivers/pwm/Makefile | 1 + drivers/pwm/pwm-sun50i-h616.c | 959 ++++++++++++++++++++++++++++++++++ 3 files changed, 972 insertions(+) create mode 100644 drivers/pwm/pwm-sun50i-h616.c diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig index 6f3147518376..66534e033761 100644 --- a/drivers/pwm/Kconfig +++ b/drivers/pwm/Kconfig @@ -736,6 +736,18 @@ config PWM_SUN4I To compile this driver as a module, choose M here: the module will be called pwm-sun4i. =20 +config PWM_SUN50I_H616 + tristate "Allwinner H616 PWM support" + depends on ARCH_SUNXI || COMPILE_TEST + depends on HAS_IOMEM && COMMON_CLK + help + Generic PWM framework driver for Allwinner H616 SoCs. + It supports generic PWM, but can also provides a plain clock. + The AC300 PHY integrated in H616 SoC needs such a clock. + + To compile this driver as a module, choose M here: the module + will be called pwm-h616. + config PWM_SUNPLUS tristate "Sunplus PWM support" depends on ARCH_SUNPLUS || COMPILE_TEST diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile index 0dc0d2b69025..a16ae9eef9e5 100644 --- a/drivers/pwm/Makefile +++ b/drivers/pwm/Makefile @@ -67,6 +67,7 @@ obj-$(CONFIG_PWM_STM32) +=3D pwm-stm32.o obj-$(CONFIG_PWM_STM32_LP) +=3D pwm-stm32-lp.o obj-$(CONFIG_PWM_STMPE) +=3D pwm-stmpe.o obj-$(CONFIG_PWM_SUN4I) +=3D pwm-sun4i.o +obj-$(CONFIG_PWM_SUN50I_H616) +=3D pwm-sun50i-h616.o obj-$(CONFIG_PWM_SUNPLUS) +=3D pwm-sunplus.o obj-$(CONFIG_PWM_TEGRA) +=3D pwm-tegra.o obj-$(CONFIG_PWM_TH1520) +=3D pwm_th1520.o diff --git a/drivers/pwm/pwm-sun50i-h616.c b/drivers/pwm/pwm-sun50i-h616.c new file mode 100644 index 000000000000..02a8e2d39f86 --- /dev/null +++ b/drivers/pwm/pwm-sun50i-h616.c @@ -0,0 +1,959 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Driver for Allwinner H616 Pulse Width Modulation Controller + * + * (C) Copyright 2025 Richard Genoud, Bootlin + * + * Based on drivers/pwm/pwm-sun4i.c with Copyright: + * + * Copyright (C) 2014 Alexandre Belloni + * + * Limitations: + * - As the channels are paired (0/1, 2/3, 4/5), they share the same clock + * source and prescaler(div_m), but they also have their own prescaler(d= iv_k) + * and bypass. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* PWM IRQ Enable Register */ +#define H616_PWM_IER 0x0 + +/* PWM IRQ Status Register */ +#define H616_PWM_ISR 0x4 + +/* PWM Capture IRQ Enable Register */ +#define H616_PWM_CIER 0x10 + +/* PWM Capture IRQ Status Register */ +#define H616_PWM_CISR 0x14 + +/* PWMCC Pairs Clock Configuration Registers */ +#define H616_PWM_XY_CLK_CR(pair) (0x20 + ((pair) * 0x4)) +#define H616_PWM_XY_CLK_CR_SRC_SHIFT 7 +#define H616_PWM_XY_CLK_CR_SRC_MASK 1 +#define H616_PWM_XY_CLK_CR_GATE_BIT 4 +#define H616_PWM_XY_CLK_CR_BYPASS_BIT(chan) ((chan) % 2 + 5) +#define H616_PWM_XY_CLK_CR_DIV_M_SHIFT 0 + +/* PWMCC Pairs Dead Zone Control Registers */ +#define H616_PWM_XY_DZ(pair) (0x30 + ((pair) * 0x4)) + +/* PWM Enable Register */ +#define H616_PWM_ENR 0x40 +#define H616_PWM_ENABLE(x) BIT(x) + +/* PWM Capture Enable Register */ +#define H616_PWM_CER 0x44 + +/* PWM Control Register */ +#define H616_PWM_CTRL_REG(chan) (0x60 + (chan) * 0x20) +#define H616_PWM_CTRL_PRESCAL_K_SHIFT 0 +#define H616_PWM_CTRL_PRESCAL_K_WIDTH 8 +#define H616_PWM_CTRL_ACTIVE_STATE BIT(8) + +/* PWM Period Register */ +#define H616_PWM_PERIOD_REG(ch) (0x64 + (ch) * 0x20) +#define H616_PWM_PERIOD_MASK GENMASK(31, 16) +#define H616_PWM_DUTY_MASK GENMASK(15, 0) +#define H616_PWM_REG_PERIOD(reg) (FIELD_GET(H616_PWM_PERIOD_MASK, reg) + 1) +#define H616_PWM_REG_DUTY(reg) FIELD_GET(H616_PWM_DUTY_MASK, reg) +#define H616_PWM_PERIOD(prd) FIELD_PREP(H616_PWM_PERIOD_MASK, (prd) - 1) +#define H616_PWM_DUTY(dty) FIELD_PREP(H616_PWM_DUTY_MASK, dty) +#define H616_PWM_PERIOD_MAX (FIELD_MAX(H616_PWM_PERIOD_MASK) + 1) + +/* PWM Count Register */ +#define H616_PWM_CNT_REG(x) (0x68 + (x) * 0x20) + +/* PWM Capture Control Register */ +#define H616_PWM_CCR(x) (0x6c + (x) * 0x20) + +/* PWM Capture Rise Lock Register */ +#define H616_PWM_CRLR(x) (0x70 + (x) * 0x20) + +/* PWM Capture Fall Lock Register */ +#define H616_PWM_CFLR(x) (0x74 + (x) * 0x20) + +#define H616_PWM_PAIR_IDX(chan) ((chan) >> 2) + +/* + * Block diagram of the PWM clock controller: + * + * _____ ______ ________ + * OSC24M --->| | | | | | + * APB1 ----->| Mux |--->| Gate |--->| /div_m |-----> H616_PWM_clock_src_xy + * |_____| |______| |________| + * ________ + * | | + * +->| /div_k |---> H616_PWM_clock_x + * | |________| + * | ______ + * | | | + * +-->| Gate |----> H616_PWM_bypass_clock_x + * | |______| + * H616_PWM_clock_src_xy ----+ ________ + * | | | + * +->| /div_k |---> H616_PWM_clock_y + * | |________| + * | ______ + * | | | + * +-->| Gate |----> H616_PWM_bypass_clock_y + * |______| + * + * NB: when the bypass is set, all the PWM logic is bypassed. + * So, the duty cycle and polarity can't be modified (we just have a clock= ). + * The bypass in PWM mode is used to achieve a 1/2 relative duty cycle wit= h the + * fastest clock. + * + * H616_PWM_clock_x/y serve for the PWM purpose. + * H616_PWM_bypass_clock_x/y serve for the clock-provider purpose. + * + */ + +/* + * Table used for /div_m (diviser before obtaining H616_PWM_clock_src_xy) + * It's actually CLK_DIVIDER_POWER_OF_TWO, but limited to /256 + */ +#define CLK_TABLE_DIV_M_ENTRY(i) { \ + .val =3D (i), .div =3D 1 << (i) \ +} + +static const struct clk_div_table clk_table_div_m[] =3D { + CLK_TABLE_DIV_M_ENTRY(0), + CLK_TABLE_DIV_M_ENTRY(1), + CLK_TABLE_DIV_M_ENTRY(2), + CLK_TABLE_DIV_M_ENTRY(3), + CLK_TABLE_DIV_M_ENTRY(4), + CLK_TABLE_DIV_M_ENTRY(5), + CLK_TABLE_DIV_M_ENTRY(6), + CLK_TABLE_DIV_M_ENTRY(7), + CLK_TABLE_DIV_M_ENTRY(8), + { /* sentinel */ } +}; + +#define H616_PWM_XY_SRC_GATE(_pair, _reg) \ +struct clk_gate gate_xy_##_pair =3D { \ + .reg =3D (void *)(_reg), \ + .bit_idx =3D H616_PWM_XY_CLK_CR_GATE_BIT, \ + .hw.init =3D &(struct clk_init_data){ \ + .ops =3D &clk_gate_ops, \ + } \ +} + +#define H616_PWM_XY_SRC_MUX(_pair, _reg) \ +struct clk_mux mux_xy_##_pair =3D { \ + .reg =3D (void *)(_reg), \ + .shift =3D H616_PWM_XY_CLK_CR_SRC_SHIFT, \ + .mask =3D H616_PWM_XY_CLK_CR_SRC_MASK, \ + .flags =3D CLK_MUX_ROUND_CLOSEST, \ + .hw.init =3D &(struct clk_init_data){ \ + .ops =3D &clk_mux_ops, \ + } \ +} + +#define H616_PWM_XY_SRC_DIV(_pair, _reg) \ +struct clk_divider rate_xy_##_pair =3D { \ + .reg =3D (void *)(_reg), \ + .shift =3D H616_PWM_XY_CLK_CR_DIV_M_SHIFT, \ + .table =3D clk_table_div_m, \ + .hw.init =3D &(struct clk_init_data){ \ + .ops =3D &clk_divider_ops, \ + } \ +} + +#define H616_PWM_X_DIV(_idx, _reg) \ +struct clk_divider rate_x_##_idx =3D { \ + .reg =3D (void *)(_reg), \ + .shift =3D H616_PWM_CTRL_PRESCAL_K_SHIFT, \ + .width =3D H616_PWM_CTRL_PRESCAL_K_WIDTH, \ + .hw.init =3D &(struct clk_init_data){ \ + .ops =3D &clk_divider_ops, \ + } \ +} + +#define H616_PWM_X_BYPASS_GATE(_idx) \ +struct clk_gate gate_x_bypass_##_idx =3D { \ + .reg =3D (void *)H616_PWM_ENR, \ + .bit_idx =3D _idx, \ + .hw.init =3D &(struct clk_init_data){ \ + .ops =3D &clk_gate_ops, \ + } \ +} + +#define H616_PWM_XY_CLK_SRC(_pair, _reg) \ + static H616_PWM_XY_SRC_MUX(_pair, _reg); \ + static H616_PWM_XY_SRC_GATE(_pair, _reg); \ + static H616_PWM_XY_SRC_DIV(_pair, _reg) + +#define H616_PWM_X_CLK(_idx) \ + static H616_PWM_X_DIV(_idx, H616_PWM_CTRL_REG(_idx)) + +#define H616_PWM_X_BYPASS_CLK(_idx) \ + H616_PWM_X_BYPASS_GATE(_idx) + +#define REF_CLK_XY_SRC(_pair) \ + { \ + .name =3D "pwm-clk-src" #_pair, \ + .mux_hw =3D &mux_xy_##_pair.hw, \ + .gate_hw =3D &gate_xy_##_pair.hw, \ + .rate_hw =3D &rate_xy_##_pair.hw, \ + } + +#define REF_CLK_X(_idx, _pair) \ + { \ + .name =3D "pwm-clk" #_idx, \ + .parent_names =3D (const char *[]){ "pwm-clk-src" #_pair }, \ + .num_parents =3D 1, \ + .rate_hw =3D &rate_x_##_idx.hw, \ + .flags =3D CLK_SET_RATE_PARENT, \ + } + +#define REF_CLK_BYPASS(_idx, _pair) \ + { \ + .name =3D "pwm-clk-bypass" #_idx, \ + .parent_names =3D (const char *[]){ "pwm-clk-src" #_pair }, \ + .num_parents =3D 1, \ + .gate_hw =3D &gate_x_bypass_##_idx.hw, \ + .flags =3D CLK_SET_RATE_PARENT, \ + } + +/* + * H616_PWM_clock_src_xy generation: + * _____ ______ ________ + * OSC24M --->| | | | | | + * APB1 ----->| Mux |--->| Gate |--->| /div_m |-----> H616_PWM_clock_src_xy + * |_____| |______| |________| + */ +H616_PWM_XY_CLK_SRC(01, H616_PWM_XY_CLK_CR(0)); +H616_PWM_XY_CLK_SRC(23, H616_PWM_XY_CLK_CR(1)); +H616_PWM_XY_CLK_SRC(45, H616_PWM_XY_CLK_CR(2)); + +/* + * H616_PWM_clock_x_div generation: + * ________ + * | | H616_PWM_clock_x/y + * H616_PWM_clock_src_xy --->| /div_k |---------------> + * |________| + */ +H616_PWM_X_CLK(0); +H616_PWM_X_CLK(1); +H616_PWM_X_CLK(2); +H616_PWM_X_CLK(3); +H616_PWM_X_CLK(4); +H616_PWM_X_CLK(5); + +/* + * H616_PWM_bypass_clock_xy generation: + * ______ + * | | + * H616_PWM_clock_src_xy ---->| Gate |-------> H616_PWM_bypass_clock_x + * |______| + * + * The gate is actually H616_PWM_ENR register. + */ +H616_PWM_X_BYPASS_CLK(0); +H616_PWM_X_BYPASS_CLK(1); +H616_PWM_X_BYPASS_CLK(2); +H616_PWM_X_BYPASS_CLK(3); +H616_PWM_X_BYPASS_CLK(4); +H616_PWM_X_BYPASS_CLK(5); + +struct clk_pwm_data { + const char *name; + const char **parent_names; + unsigned int num_parents; + struct clk_hw *mux_hw; + struct clk_hw *rate_hw; + struct clk_hw *gate_hw; + unsigned long flags; +}; + +#define CLK_BYPASS(h616chip, ch) ((h616chip)->data->npwm + (ch)) +#define CLK_XY_SRC_IDX(h616chip, ch) ((h616chip)->data->npwm * 2 + ((ch) >= > 1)) +static struct clk_pwm_data pwmcc_data[] =3D { + REF_CLK_X(0, 01), + REF_CLK_X(1, 01), + REF_CLK_X(2, 23), + REF_CLK_X(3, 23), + REF_CLK_X(4, 45), + REF_CLK_X(5, 45), + REF_CLK_BYPASS(0, 01), + REF_CLK_BYPASS(1, 01), + REF_CLK_BYPASS(2, 23), + REF_CLK_BYPASS(3, 23), + REF_CLK_BYPASS(4, 45), + REF_CLK_BYPASS(5, 45), + REF_CLK_XY_SRC(01), + REF_CLK_XY_SRC(23), + REF_CLK_XY_SRC(45), + { /* sentinel */ } +}; + +enum h616_pwm_mode { + H616_PWM_MODE_NONE, + H616_PWM_MODE_PWM, + H616_PWM_MODE_CLK, +}; + +struct h616_pwm_data { + unsigned int npwm; +}; + +struct h616_pwm_channel { + struct clk *pwm_clk; + enum h616_pwm_mode mode; +}; + +struct clk_pwm_pdata { + struct clk_hw_onecell_data *hw_data; + spinlock_t lock; + void __iomem *reg; +}; + +struct h616_pwm_chip { + struct clk_pwm_pdata *clk_pdata; + struct h616_pwm_channel *channels; + struct clk *bus_clk; + struct reset_control *rst; + void __iomem *base; + const struct h616_pwm_data *data; +}; + +struct h616_pwm_waveform { + u8 enabled:1; + u8 active_state:1; + u8 bypass_en:1; + u16 duty_ticks; + u32 period_ticks; + unsigned long clk_rate; +}; + +static inline struct h616_pwm_chip *h616_pwm_from_chip(const struct pwm_ch= ip *chip) +{ + return pwmchip_get_drvdata(chip); +} + +static inline u32 h616_pwm_readl(struct h616_pwm_chip *h616chip, + unsigned long offset) +{ + return readl(h616chip->base + offset); +} + +static inline void h616_pwm_writel(struct h616_pwm_chip *h616chip, + u32 val, unsigned long offset) +{ + writel(val, h616chip->base + offset); +} + +static void h616_pwm_set_bypass(struct h616_pwm_chip *h616chip, + unsigned int idx, bool en_bypass) +{ + unsigned long flags, reg_offset; + u32 val; + + spin_lock_irqsave(&h616chip->clk_pdata->lock, flags); + + reg_offset =3D H616_PWM_XY_CLK_CR(H616_PWM_PAIR_IDX(idx)); + val =3D h616_pwm_readl(h616chip, reg_offset); + if (en_bypass) + val |=3D BIT(H616_PWM_XY_CLK_CR_BYPASS_BIT(idx)); + else + val &=3D ~BIT(H616_PWM_XY_CLK_CR_BYPASS_BIT(idx)); + + h616_pwm_writel(h616chip, val, reg_offset); + + spin_unlock_irqrestore(&h616chip->clk_pdata->lock, flags); +} + +static int h616_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) +{ + struct h616_pwm_chip *h616chip =3D h616_pwm_from_chip(chip); + struct h616_pwm_channel *chan =3D &h616chip->channels[pwm->hwpwm]; + + scoped_guard(spinlock_irqsave, &h616chip->clk_pdata->lock) { + if (chan->mode =3D=3D H616_PWM_MODE_CLK) + return -EBUSY; + chan->mode =3D H616_PWM_MODE_PWM; + } + + return clk_prepare_enable(chan->pwm_clk); +} + +static void h616_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) +{ + struct h616_pwm_chip *h616chip =3D h616_pwm_from_chip(chip); + struct h616_pwm_channel *chan =3D &h616chip->channels[pwm->hwpwm]; + + clk_disable_unprepare(chan->pwm_clk); + chan->mode =3D H616_PWM_MODE_NONE; +} + +static int h616_pwm_read_waveform(struct pwm_chip *chip, + struct pwm_device *pwm, + void *_wfhw) +{ + struct h616_pwm_waveform *wfhw =3D _wfhw; + struct h616_pwm_chip *h616chip =3D h616_pwm_from_chip(chip); + struct h616_pwm_channel *chan =3D &h616chip->channels[pwm->hwpwm]; + u32 val; + + wfhw->clk_rate =3D clk_get_rate(chan->pwm_clk); + + val =3D h616_pwm_readl(h616chip, H616_PWM_ENR); + wfhw->enabled =3D !!(H616_PWM_ENABLE(pwm->hwpwm) & val); + + val =3D h616_pwm_readl(h616chip, H616_PWM_XY_CLK_CR(H616_PWM_PAIR_IDX(pwm= ->hwpwm))); + wfhw->bypass_en =3D !!(val & BIT(H616_PWM_XY_CLK_CR_BYPASS_BIT(pwm->hwpwm= ))); + + val =3D h616_pwm_readl(h616chip, H616_PWM_CTRL_REG(pwm->hwpwm)); + wfhw->active_state =3D !!(val & H616_PWM_CTRL_ACTIVE_STATE); + + val =3D h616_pwm_readl(h616chip, H616_PWM_PERIOD_REG(pwm->hwpwm)); + wfhw->duty_ticks =3D H616_PWM_REG_DUTY(val); + wfhw->period_ticks =3D H616_PWM_REG_PERIOD(val); + + dev_dbg(pwmchip_parent(chip), + "pwm%u: %s bypass: %s polarity: %s clk_rate=3D%lu period_ticks=3D%u duty= _ticks=3D%u\n", + pwm->hwpwm, + wfhw->enabled ? "enabled" : "disabled", + wfhw->bypass_en ? "enabled" : "disabled", + wfhw->active_state ? "normal" : "inversed", + wfhw->clk_rate, wfhw->period_ticks, wfhw->duty_ticks); + + return 0; +} + +static int h616_pwm_round_waveform_fromhw(struct pwm_chip *chip, + struct pwm_device *pwm, + const void *_wfhw, + struct pwm_waveform *wf) +{ + const struct h616_pwm_waveform *wfhw =3D _wfhw; + u64 tmp, resolution; + + dev_dbg(pwmchip_parent(chip), + "pwm%u: %s bypass: %s polarity: %s clk_rate=3D%lu period_ticks=3D%u duty= _ticks=3D%u\n", + pwm->hwpwm, + wfhw->enabled ? "enabled" : "disabled", + wfhw->bypass_en ? "enabled" : "disabled", + wfhw->active_state ? "normal" : "inversed", + wfhw->clk_rate, wfhw->period_ticks, wfhw->duty_ticks); + + wf->duty_offset_ns =3D 0; + + if (!wfhw->enabled || !wfhw->clk_rate) { + wf->period_length_ns =3D 0; + wf->duty_length_ns =3D 0; + return 0; + } + + if (wfhw->bypass_en) { + wf->period_length_ns =3D DIV_ROUND_UP_ULL(NSEC_PER_SEC, + wfhw->clk_rate); + wf->duty_length_ns =3D DIV_ROUND_UP_ULL(wf->period_length_ns, 2); + return 0; + } + + tmp =3D NSEC_PER_SEC * (u64)wfhw->period_ticks; + wf->period_length_ns =3D DIV_ROUND_UP_ULL(tmp, wfhw->clk_rate); + + tmp =3D NSEC_PER_SEC * (u64)wfhw->duty_ticks; + wf->duty_length_ns =3D DIV_ROUND_UP_ULL(tmp, wfhw->clk_rate); + if (!wfhw->active_state) { + /* + * For inverted polarity, we have to fix cases where + * computed duty_length_ns > requested duty_length_ns + * For that, we subtract the actual resolution of the PWM + * registers + */ + wf->duty_offset_ns =3D wf->duty_length_ns; + wf->duty_length_ns =3D wf->period_length_ns - wf->duty_length_ns; + + resolution =3D DIV_ROUND_UP_ULL(NSEC_PER_SEC, wfhw->clk_rate); + + if (wf->duty_offset_ns >=3D resolution) + wf->duty_offset_ns -=3D resolution; + } + + dev_dbg(pwmchip_parent(chip), + "pwm%u period_length_ns=3D%llu duty_length_ns=3D%llu duty_offset_ns=3D%l= lu\n", + pwm->hwpwm, wf->period_length_ns, wf->duty_length_ns, + wf->duty_offset_ns); + + return 0; +} + +static int h616_pwm_write_waveform(struct pwm_chip *chip, + struct pwm_device *pwm, const void *_wfhw) +{ + const struct h616_pwm_waveform *wfhw =3D _wfhw; + struct h616_pwm_chip *h616chip =3D h616_pwm_from_chip(chip); + struct h616_pwm_channel *chan =3D &h616chip->channels[pwm->hwpwm]; + unsigned long flags; + u32 val; + int ret; + + ret =3D clk_set_rate(chan->pwm_clk, wfhw->clk_rate); + if (ret) + return ret; + + h616_pwm_set_bypass(h616chip, pwm->hwpwm, wfhw->bypass_en); + + val =3D h616_pwm_readl(h616chip, H616_PWM_CTRL_REG(pwm->hwpwm)); + if (wfhw->active_state) + val |=3D H616_PWM_CTRL_ACTIVE_STATE; + else + val &=3D ~H616_PWM_CTRL_ACTIVE_STATE; + h616_pwm_writel(h616chip, val, H616_PWM_CTRL_REG(pwm->hwpwm)); + + val =3D H616_PWM_DUTY(wfhw->duty_ticks); + val |=3D H616_PWM_PERIOD(wfhw->period_ticks); + h616_pwm_writel(h616chip, val, H616_PWM_PERIOD_REG(pwm->hwpwm)); + + spin_lock_irqsave(&h616chip->clk_pdata->lock, flags); + + val =3D h616_pwm_readl(h616chip, H616_PWM_ENR); + if (wfhw->enabled) + val |=3D H616_PWM_ENABLE(pwm->hwpwm); + else + val &=3D ~H616_PWM_ENABLE(pwm->hwpwm); + h616_pwm_writel(h616chip, val, H616_PWM_ENR); + + spin_unlock_irqrestore(&h616chip->clk_pdata->lock, flags); + + return 0; +} + +static int h616_pwm_round_waveform_tohw(struct pwm_chip *chip, + struct pwm_device *pwm, + const struct pwm_waveform *wf, + void *_wfhw) +{ + struct h616_pwm_chip *h616chip =3D h616_pwm_from_chip(chip); + struct h616_pwm_channel *chan =3D &h616chip->channels[pwm->hwpwm]; + struct h616_pwm_waveform *wfhw =3D _wfhw; + unsigned long max_rate; + long calc_rate; + u64 period_ratio, double_duty_ratio, freq, duty_cycle; + + dev_dbg(pwmchip_parent(chip), + "pwm%u period_length_ns=3D%llu duty_length_ns=3D%llu duty_offset_ns=3D%l= lu\n", + pwm->hwpwm, wf->period_length_ns, wf->duty_length_ns, + wf->duty_offset_ns); + + if (wf->period_length_ns =3D=3D 0) { + wfhw->enabled =3D 0; + return 0; + } + + wfhw->enabled =3D 1; + + duty_cycle =3D wf->duty_length_ns; + if (wf->duty_length_ns + wf->duty_offset_ns < wf->period_length_ns) + wfhw->active_state =3D 1; + else + wfhw->active_state =3D 0; + + dev_dbg(pwmchip_parent(chip), "polarity: %s\n", + wfhw->active_state ? "normal" : "inversed"); + + /* + * Lowest possible period case: + * Without bypass, the lowest possible period is when: + * duty cycle =3D 1 and period cycle =3D 2 (0x10001 in period register) + * E.g. if the input clock is 100MHz, we have a lowest period of 20ns. + * Now, with the bypass, the period register is ignored and we directly + * have the 100MHz clock as PWM output, that can act as a 10ns period + * with 5ns duty. + * So, to detect this lowest period case, just get the maximum possible + * rate from chan->pwm_clk and compare it with requested period and + * duty_cycle. + * + * But, to get the maximum possible rate, we have to use U32_MAX instead + * of (unsigned long)-1. + * This is because clk_round_rate() uses ultimately DIV_ROUND_UP_ULL() + * that in turn do_div(n,base). And base is uint32_t divisor. + */ + max_rate =3D clk_round_rate(chan->pwm_clk, U32_MAX); + + dev_dbg(pwmchip_parent(chip), "max_rate: %ld Hz\n", max_rate); + + period_ratio =3D mul_u64_u64_div_u64(wf->period_length_ns, + max_rate, NSEC_PER_SEC); + double_duty_ratio =3D mul_u64_u64_div_u64(duty_cycle, (u64)max_rate * 2, + NSEC_PER_SEC); + if (period_ratio =3D=3D 1) { + if (double_duty_ratio =3D=3D 0) + /* requested period and duty are too small */ + return -EINVAL; + /* + * If the requested period is to small to be generated by the + * PWM, but matches the highest clock with a + * duty_cycle >=3D period*2, just bypass the PWM logic + */ + freq =3D div64_u64(NSEC_PER_SEC, wf->period_length_ns); + wfhw->bypass_en =3D true; + } else { + wfhw->bypass_en =3D false; + freq =3D div64_u64(NSEC_PER_SEC * (u64)H616_PWM_PERIOD_MAX, + wf->period_length_ns); + /* + * Same remark as above, this is to prevent a value to big for + * clk_round_rate() to handle + */ + if (freq > U32_MAX) + freq =3D U32_MAX; + } + + dev_dbg(pwmchip_parent(chip), "bypass: %s\n", + wfhw->bypass_en ? "enabled" : "disabled"); + + calc_rate =3D clk_round_rate(chan->pwm_clk, freq); + if (calc_rate <=3D 0) + return calc_rate ? calc_rate : -EINVAL; + + dev_dbg(pwmchip_parent(chip), "calc_rate: %ld Hz\n", calc_rate); + + wfhw->period_ticks =3D mul_u64_u64_div_u64(calc_rate, + wf->period_length_ns, + NSEC_PER_SEC); + if (wfhw->period_ticks > H616_PWM_PERIOD_MAX) + wfhw->period_ticks =3D H616_PWM_PERIOD_MAX; + + /* min value in period register is 1 */ + if (wfhw->period_ticks =3D=3D 0) + return -EINVAL; + + wfhw->duty_ticks =3D mul_u64_u64_div_u64(calc_rate, duty_cycle, + NSEC_PER_SEC); + + if (wfhw->duty_ticks > wfhw->period_ticks) + wfhw->duty_ticks =3D wfhw->period_ticks; + + if (!wfhw->active_state) + wfhw->duty_ticks =3D wfhw->period_ticks - wfhw->duty_ticks; + + dev_dbg(pwmchip_parent(chip), + "pwm%u period_ticks=3D%u duty_cycle=3D%llu duty_ticks=3D%u\n", + pwm->hwpwm, wfhw->period_ticks, duty_cycle, wfhw->duty_ticks); + + wfhw->clk_rate =3D calc_rate; + + return 0; +} + +static const struct pwm_ops h616_pwm_ops =3D { + .request =3D h616_pwm_request, + .free =3D h616_pwm_free, + .sizeof_wfhw =3D sizeof(struct h616_pwm_waveform), + .round_waveform_tohw =3D h616_pwm_round_waveform_tohw, + .round_waveform_fromhw =3D h616_pwm_round_waveform_fromhw, + .read_waveform =3D h616_pwm_read_waveform, + .write_waveform =3D h616_pwm_write_waveform, +}; + +static struct clk_hw *h616_pwm_of_clk_get(struct of_phandle_args *clkspec, + void *data) +{ + struct h616_pwm_chip *h616chip =3D data; + struct clk_hw_onecell_data *hw_data =3D h616chip->clk_pdata->hw_data; + unsigned int idx =3D clkspec->args[0]; + struct h616_pwm_channel *chan; + struct clk_hw *ret_clk =3D NULL; + unsigned long flags; + + if (idx >=3D h616chip->data->npwm) + return ERR_PTR(-EINVAL); + + chan =3D &h616chip->channels[idx]; + + spin_lock_irqsave(&h616chip->clk_pdata->lock, flags); + + if (chan->mode =3D=3D H616_PWM_MODE_PWM) { + ret_clk =3D ERR_PTR(-EBUSY); + } else { + chan->mode =3D H616_PWM_MODE_CLK; + ret_clk =3D hw_data->hws[CLK_BYPASS(h616chip, idx)]; + } + spin_unlock_irqrestore(&h616chip->clk_pdata->lock, flags); + + if (IS_ERR(ret_clk)) + goto out; + + h616_pwm_set_bypass(h616chip, idx, true); +out: + return ret_clk; +} + +static int h616_add_composite_clk(struct clk_pwm_data *data, + void __iomem *reg, spinlock_t *lock, + struct device *dev, struct clk_hw **hw) +{ + const struct clk_ops *mux_ops =3D NULL, *gate_ops =3D NULL, *rate_ops =3D= NULL; + struct clk_hw *mux_hw =3D NULL, *gate_hw =3D NULL, *rate_hw =3D NULL; + struct device_node *node =3D dev->of_node; + + if (data->mux_hw) { + struct clk_mux *mux; + + mux_hw =3D data->mux_hw; + mux =3D to_clk_mux(mux_hw); + mux->lock =3D lock; + mux_ops =3D mux_hw->init->ops; + mux->reg =3D (u64)mux->reg + reg; + } + + if (data->gate_hw) { + struct clk_gate *gate; + + gate_hw =3D data->gate_hw; + gate =3D to_clk_gate(gate_hw); + gate->lock =3D lock; + gate_ops =3D gate_hw->init->ops; + gate->reg =3D (u64)gate->reg + reg; + } + + if (data->rate_hw) { + struct clk_divider *rate; + + rate_hw =3D data->rate_hw; + rate =3D to_clk_divider(rate_hw); + rate_ops =3D rate_hw->init->ops; + rate->lock =3D lock; + rate->reg =3D (u64)rate->reg + reg; + + if (rate->table) { + const struct clk_div_table *clkt; + int table_size =3D 0; + + for (clkt =3D rate->table; clkt->div; clkt++) + table_size++; + rate->width =3D order_base_2(table_size); + } + } + + /* + * Retrieve the parent clock names from DTS for pwm-clk-srcxy + */ + if (!data->parent_names) { + data->num_parents =3D of_clk_get_parent_count(node); + if (data->num_parents =3D=3D 0) + return -ENOENT; + + data->parent_names =3D devm_kzalloc(dev, + sizeof(*data->parent_names), + GFP_KERNEL); + for (unsigned int i =3D 0; i < data->num_parents; i++) + data->parent_names[i] =3D of_clk_get_parent_name(node, i); + } + + *hw =3D clk_hw_register_composite(dev, data->name, data->parent_names, + data->num_parents, mux_hw, + mux_ops, rate_hw, rate_ops, + gate_hw, gate_ops, data->flags); + + return PTR_ERR_OR_ZERO(*hw); +} + +static int h616_pwm_init_clocks(struct platform_device *pdev, + struct h616_pwm_chip *h616chip) +{ + struct clk_pwm_pdata *pdata; + struct device *dev =3D &pdev->dev; + int num_clocks =3D 0; + int ret; + + pdata =3D devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); + if (!pdata) + return dev_err_probe(dev, -ENOMEM, + "Failed to allocate clk_pwm_pdata\n"); + + while (pwmcc_data[num_clocks].name) + num_clocks++; + + pdata->hw_data =3D devm_kzalloc(dev, struct_size(pdata->hw_data, hws, num= _clocks), + GFP_KERNEL); + if (!pdata->hw_data) + return dev_err_probe(dev, -ENOMEM, + "Failed to allocate hw clocks\n"); + + pdata->hw_data->num =3D num_clocks; + pdata->reg =3D h616chip->base; + + spin_lock_init(&pdata->lock); + + for (int i =3D 0; i < num_clocks; i++) { + struct clk_hw **hw =3D &pdata->hw_data->hws[i]; + + ret =3D h616_add_composite_clk(&pwmcc_data[i], pdata->reg, + &pdata->lock, dev, hw); + if (ret) { + dev_err_probe(dev, ret, + "Failed to register hw clock %s\n", + pwmcc_data[i].name); + for (i--; i >=3D 0; i--) + clk_hw_unregister_composite(pdata->hw_data->hws[i]); + return ret; + } + } + + h616chip->clk_pdata =3D pdata; + + return 0; +} + +static int h616_pwm_probe(struct platform_device *pdev) +{ + const struct h616_pwm_data *data; + struct device *dev =3D &pdev->dev; + struct h616_pwm_chip *h616chip; + struct pwm_chip *chip; + int ret; + + data =3D of_device_get_match_data(dev); + if (!data) + return dev_err_probe(dev, -ENODEV, + "Missing specific data structure\n"); + + chip =3D devm_pwmchip_alloc(dev, data->npwm, sizeof(*h616chip)); + if (IS_ERR(chip)) + return dev_err_probe(dev, PTR_ERR(chip), + "Failed to allocate pwmchip\n"); + + h616chip =3D h616_pwm_from_chip(chip); + h616chip->data =3D data; + h616chip->base =3D devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(h616chip->base)) + return dev_err_probe(dev, PTR_ERR(h616chip->base), + "Failed to get PWM base address\n"); + + h616chip->bus_clk =3D devm_clk_get_enabled(dev, "bus"); + if (IS_ERR(h616chip->bus_clk)) + return dev_err_probe(dev, PTR_ERR(h616chip->bus_clk), + "Failed to get bus clock\n"); + + h616chip->channels =3D devm_kmalloc_array(dev, data->npwm, + sizeof(*(h616chip->channels)), + GFP_KERNEL); + if (!h616chip->channels) + return dev_err_probe(dev, -ENOMEM, + "Failed to allocate %d channels array\n", + data->npwm); + + h616chip->rst =3D devm_reset_control_get_shared(dev, NULL); + if (IS_ERR(h616chip->rst)) + return dev_err_probe(dev, PTR_ERR(h616chip->rst), + "Failed to get reset control\n"); + + chip->ops =3D &h616_pwm_ops; + + ret =3D h616_pwm_init_clocks(pdev, h616chip); + if (ret) + return ret; + + for (unsigned int i =3D 0; i < data->npwm; i++) { + struct h616_pwm_channel *chan =3D &h616chip->channels[i]; + struct clk_hw **hw =3D &h616chip->clk_pdata->hw_data->hws[i]; + + chan->pwm_clk =3D devm_clk_hw_get_clk(dev, *hw, NULL); + if (IS_ERR(chan->pwm_clk)) { + ret =3D dev_err_probe(dev, PTR_ERR(chan->pwm_clk), + "Failed to register PWM clock %d\n", i); + goto err_get_clk; + } + chan->mode =3D H616_PWM_MODE_NONE; + } + + ret =3D devm_of_clk_add_hw_provider(dev, h616_pwm_of_clk_get, h616chip); + if (ret) { + dev_err_probe(dev, ret, "Failed to add HW clock provider\n"); + goto err_add_clk_provider; + } + + /* Deassert reset */ + ret =3D reset_control_deassert(h616chip->rst); + if (ret) { + dev_err_probe(dev, ret, "Cannot deassert reset control\n"); + goto err_ctrl_deassert; + } + + ret =3D pwmchip_add(chip); + if (ret < 0) { + dev_err_probe(dev, ret, "Failed to add PWM chip\n"); + goto err_pwm_add; + } + + platform_set_drvdata(pdev, chip); + + return 0; + +err_pwm_add: + reset_control_assert(h616chip->rst); + +err_ctrl_deassert: +err_add_clk_provider: +err_get_clk: + for (unsigned int i =3D 0; i < h616chip->clk_pdata->hw_data->num; i++) + clk_hw_unregister_composite(h616chip->clk_pdata->hw_data->hws[i]); + + return ret; +} + +static void h616_pwm_remove(struct platform_device *pdev) +{ + struct pwm_chip *chip =3D platform_get_drvdata(pdev); + struct h616_pwm_chip *h616chip =3D h616_pwm_from_chip(chip); + + pwmchip_remove(chip); + + reset_control_assert(h616chip->rst); + + for (unsigned int i =3D 0; i < h616chip->clk_pdata->hw_data->num; i++) + clk_hw_unregister_composite(h616chip->clk_pdata->hw_data->hws[i]); + +} + +static const struct h616_pwm_data sun50i_h616_pwm_data =3D { + .npwm =3D 6, +}; + +static const struct of_device_id h616_pwm_dt_ids[] =3D { + { + .compatible =3D "allwinner,sun50i-h616-pwm", + .data =3D &sun50i_h616_pwm_data, + }, { + /* sentinel */ + } +}; +MODULE_DEVICE_TABLE(of, h616_pwm_dt_ids); + +static struct platform_driver h616_pwm_driver =3D { + .driver =3D { + .name =3D "h616-pwm", + .of_match_table =3D h616_pwm_dt_ids, + }, + .probe =3D h616_pwm_probe, + .remove =3D h616_pwm_remove, +}; +module_platform_driver(h616_pwm_driver); + +MODULE_AUTHOR("Richard Genoud "); +MODULE_DESCRIPTION("Allwinner H616 PWM driver"); +MODULE_LICENSE("GPL"); --=20 2.47.3 From nobody Sat Feb 7 08:45:07 2026 Received: from smtpout-03.galae.net (smtpout-03.galae.net [185.246.85.4]) (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 55E61354AE7 for ; Fri, 23 Jan 2026 09:33:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.85.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769160843; cv=none; b=ASNI99yqbetliem4tnRm9hi00zsJAFW/LElxUIqv2OKWsXIe2Q4bWqBOL1Cj7N3XKEgAUgsDX+y7J0UTYUYIv995ffBSyPpB5sto7h7vFL4siSi7mK8t4TZQeBYfi+SrzjFmwvVrqq0LdTnjFHtQUipT3yBfZtxO2SqnBX1W1i0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769160843; c=relaxed/simple; bh=QCZv44dpt1wXV5t639ETH/9OXxUw6V20Cbu8oX8vMHM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eNK+5tU9pZ9tH+im+TqD4DLF/b9wNxpurAleCaNTLPXtxiMvIuLem2qtnOin0AU1ltaKyVTOXuHJE1NqEhOA2F427x/fV/cto2iaHCiHeScCW4RQYcLe/QNKNAOK4EuKujYDXPh64K0++2bpj4SXsnuBJyz6KH18XkKjZkofWTY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=NVNPbezp; arc=none smtp.client-ip=185.246.85.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="NVNPbezp" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-03.galae.net (Postfix) with ESMTPS id 2AAD54E42217; Fri, 23 Jan 2026 09:33:51 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 0036A6070A; Fri, 23 Jan 2026 09:33:51 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 85B37119A879D; Fri, 23 Jan 2026 10:33:48 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1769160829; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=aRHUhUQElWm45tRxPNhFePFW/HsuYpmnmVm/z34Giec=; b=NVNPbezpnmAIsby6thuM0qbC03Y0Zf96x+ESj82VFrPLinMcbeAI1342sZATNE9BPxtwjl 5bPXo4z+FRo+7IXjz94KPBAVKA0JAoQ1sqvb0MfxLVm6nQbVOa1BAEiI3ixbYeCjU9Tp6T TKuLiBeYcp67MT33RCFitDZEFj6NSogB65cGX8QjkvAiSuIzfTz7pVzo4qVBYEaf66lhnQ MjBPhOr7ap5MQRqGTgdMWEwNBijF3DNGr0lYdR5djFB/m0iQ3OJKf9drY975KiSPozBs3h eqEfKRK2e+4cs6/vnHGBqKANILyPXZImL6I5IUIBKpFXBrn5wQjF3unBholQ1Q== From: Richard Genoud To: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Philipp Zabel Cc: Paul Kocialkowski , Thomas Petazzoni , linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org, Richard Genoud Subject: [PATCH v3 3/4] arm64: dts: allwinner: h616: add PWM controller Date: Fri, 23 Jan 2026 10:33:21 +0100 Message-ID: <20260123093322.1327389-4-richard.genoud@bootlin.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260123093322.1327389-1-richard.genoud@bootlin.com> References: <20260123093322.1327389-1-richard.genoud@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Content-Type: text/plain; charset="utf-8" The H616 has a PWM controller that can provide PWM signals, but also plain clocks. Add the PWM controller node and pins in the device tree. Signed-off-by: Richard Genoud --- .../arm64/boot/dts/allwinner/sun50i-h616.dtsi | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/arch/arm64/boot/dts/allwinner/sun50i-h616.dtsi b/arch/arm64/bo= ot/dts/allwinner/sun50i-h616.dtsi index 8d1110c14bad..1c7628a6e4bb 100644 --- a/arch/arm64/boot/dts/allwinner/sun50i-h616.dtsi +++ b/arch/arm64/boot/dts/allwinner/sun50i-h616.dtsi @@ -236,6 +236,17 @@ watchdog: watchdog@30090a0 { clocks =3D <&osc24M>; }; =20 + pwm: pwm@300a000 { + compatible =3D "allwinner,sun50i-h616-pwm"; + reg =3D <0x0300a000 0x400>; + clocks =3D <&osc24M>, <&ccu CLK_BUS_PWM>; + clock-names =3D "mod", "bus"; + resets =3D <&ccu RST_BUS_PWM>; + #pwm-cells =3D <3>; + #clock-cells =3D <1>; + status =3D "disabled"; + }; + pio: pinctrl@300b000 { compatible =3D "allwinner,sun50i-h616-pinctrl"; reg =3D <0x0300b000 0x400>; @@ -340,6 +351,42 @@ nand_rb1_pin: nand-rb1-pin { bias-pull-up; }; =20 + /omit-if-no-ref/ + pwm0_pin: pwm0-pin { + pins =3D "PD28"; + function =3D "pwm0"; + }; + + /omit-if-no-ref/ + pwm1_pin: pwm1-pin { + pins =3D "PG19"; + function =3D "pwm1"; + }; + + /omit-if-no-ref/ + pwm2_pin: pwm2-pin { + pins =3D "PH2"; + function =3D "pwm2"; + }; + + /omit-if-no-ref/ + pwm3_pin: pwm3-pin { + pins =3D "PH0"; + function =3D "pwm3"; + }; + + /omit-if-no-ref/ + pwm4_pin: pwm4-pin { + pins =3D "PI14"; + function =3D "pwm4"; + }; + + /omit-if-no-ref/ + pwm5_pin: pwm5-pin { + pins =3D "PA12"; + function =3D "pwm5"; + }; + /omit-if-no-ref/ spi0_pins: spi0-pins { pins =3D "PC0", "PC2", "PC4"; --=20 2.47.3 From nobody Sat Feb 7 08:45:07 2026 Received: from smtpout-04.galae.net (smtpout-04.galae.net [185.171.202.116]) (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 A70B236A01B; Fri, 23 Jan 2026 09:33:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.171.202.116 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769160843; cv=none; b=FMGExUhahjvkoGuwcQ2WuiYfHyctatU46D09J25J1TSUZvkI4xgZySgedIBLpH5SxL1hnsE6fTO1T2Vj0I8S3b82upoAjLXlEtSqnP4waZCumytV30g8NGP6FZnYcqF23h9rEifmIqAWEgF143DMrWPen+A4jHj8et8QcNBkXMA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769160843; c=relaxed/simple; bh=yPdMZHCd+odB+tMLYC+rVsxO4J1sPgCZ6HLjvUDlrWw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D8+BLJil07X7u6ndHZc/zdYphQOOG0JWa24DNn8dwrGYbGvk8fzMCnqAlZF7AXGGQLawDCoJy0t3vMa3xD6Ehwg2o3YXIEXT1ZLeZPXskTXtacsVWUEJyW/eUQesXHxVlJ71vOxE1sNaoip+t0zMGdkdpeVoDfwJTX5q1lqRzqA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=SBj3gjtJ; arc=none smtp.client-ip=185.171.202.116 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="SBj3gjtJ" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-04.galae.net (Postfix) with ESMTPS id F3CD8C21ABE; Fri, 23 Jan 2026 09:33:52 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id C68606070A; Fri, 23 Jan 2026 09:33:52 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 742EF119A879B; Fri, 23 Jan 2026 10:33:50 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1769160831; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=6HdXNZ3Nba+GjaEjoZsm3NjZLI9rre4BgbexuF1mMZI=; b=SBj3gjtJo/1rgjyDl7/r5f9nceA8TyQzz6LvG0/gA6DDvoG+96GwNz7Kv6prMAItwE0JKX WdLOrIQ7+9MDcHUVFxpUPsliWfeMACv6NKH1l6zqyuogefVe2KqXYyePwE+IZs2Q2i3pCh YoKw1eecQLU5X5yGwfEiEkDgRcK/inTj36l5jF67Sc2UUGHI5NZ20mQ06GOtLqF1FkI0Pn F/yZSP6oQs3oNrjwL5wsfAU0qUSUyM3/FLbcOQ2ADrO34wXMCF451TsyGXF7JOi6KPAmFz +gSeJIdNrKn5CPY1hyaMrSIKM2Xnuxyxrqv7DorfreZX3yV/14o8E9rA4Xwlyw== From: Richard Genoud To: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Philipp Zabel Cc: Paul Kocialkowski , Thomas Petazzoni , linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org, Richard Genoud Subject: [PATCH v3 4/4] MAINTAINERS: Add entry on Allwinner H616 PWM driver Date: Fri, 23 Jan 2026 10:33:22 +0100 Message-ID: <20260123093322.1327389-5-richard.genoud@bootlin.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260123093322.1327389-1-richard.genoud@bootlin.com> References: <20260123093322.1327389-1-richard.genoud@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Content-Type: text/plain; charset="utf-8" Add myself as maintainer of Allwinner H616 PWM driver and device-tree bindings. Signed-off-by: Richard Genoud --- MAINTAINERS | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 765ad2daa218..ac7b07f4eddf 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -903,6 +903,11 @@ S: Maintained F: Documentation/devicetree/bindings/sound/allwinner,sun50i-h6-dmic.yaml F: sound/soc/sunxi/sun50i-dmic.c =20 +ALLWINNER H616 PWM DRIVER +M: Richard Genoud +S: Maintained +F: drivers/pwm/pwm-sun50i-h616.c + ALLWINNER HARDWARE SPINLOCK SUPPORT M: Wilken Gottwalt S: Maintained --=20 2.47.3