From nobody Mon Nov 25 17:52:37 2024 Received: from mail-oo1-f43.google.com (mail-oo1-f43.google.com [209.85.161.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 183301C4A03; Fri, 25 Oct 2024 08:07:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729843649; cv=none; b=QRWb/dB7mGwv1kCY2cTYr+IOwpIXpnGGrNqlkQGWHj+2wQNng23cSmGTeYALkpUzZ30uMm+F2EjWCaa1vLxx2OBD9LHqFQ68I24S6yKnXUsmgN+YtPmOxSRYXlZWzm0PxkGMatCPrhicjXG0fOJaMVlhtH9HBBFks9QbX5/gYaA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729843649; c=relaxed/simple; bh=3p4yGURKRybyWYfqJv7wifGGEWBo97x5AmEQOI4GZSY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XUbZtYXQ9MsrUPOpFAIbyfVp0yQAA/mame1J000D834kciHnm9wJBwk+E7GJIiY+HsMk1X0Wqh9/u5KkOp4EaKaYrOfp1OHrvukfMCRYvMYJgxNi1wQ/XTa7WMKJKMRao/l0wZBjO03+FWc8g79kr/XVZNd0RCk1SjYztZr2N6E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ceuzB4Lm; arc=none smtp.client-ip=209.85.161.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ceuzB4Lm" Received: by mail-oo1-f43.google.com with SMTP id 006d021491bc7-5ebc03951abso912422eaf.2; Fri, 25 Oct 2024 01:07:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729843646; x=1730448446; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kROaOIt5szJgAa68Sy5g7p0dogkgu9Z3CSKDjRWOQKY=; b=ceuzB4LmeeZoIY9RTtYShamnaO1BC1E0NE/y/DlcH0jFcXfsydpUPJHLQqP85SjY74 V0NVquz6INjS+MRh8QQVtKIffrRxvDRMmziUCtrR8xp4LVMxGL9f4CRzz/jcdTkJ3p2H wmZwLVGiTq9XlXvBUwtK+UnKRoML/kN9pnvr+yVF9EwLb/RnlGPTiaI92f6SQr0jYr2x zw0+TRj+quBd1bGuciCiRldT3SKeKFQ3ygLcCIi/M+vk/nUYU2qKvNiDnESsbEMfs97c boFQbJ5naaNiXSaRETA4HYn4a/FbDL/PNUDIuHUACoLJH+h6DIlNI0ygFf+yhnWFXlmm /frQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729843646; x=1730448446; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kROaOIt5szJgAa68Sy5g7p0dogkgu9Z3CSKDjRWOQKY=; b=BToUDJq5PMD9GgRVP14XSwhxH8cjLtqmPrLf4wixkEzfS4r/l6lpmjkW/JuIYf4OI5 CN8ZFJemCstD0gLcP6o97OlxHQQKIdGYc2522rus0bOveiRX7JXiZ7/2GDRaAhzamxFD uWZLFFAFZqtx4K0w7+s/jpVIoPlYZm5pGqtNP1Qz6ZWmvmo3xD1osdALmE0nP7kNo8vJ DmGCiPGDgYvlMndXHrNq20xl26JiqYtuj6pN0XKhtIY3JdZDJj4qRmfM7GSoWei+9MNR kZYiZD4JLy8wPSW33t3z+1Q34yBQGU1Scy6zMX0bHiRB7EdQ4ehpTyNKsBc93Fjlxsrf q+3Q== X-Forwarded-Encrypted: i=1; AJvYcCVAIxpFPynKAHtAPFw0MEN+oR91D4n6qxlrf2iN4l3C/FosGy2LQ+4+9rNfROQXzLRugiI4t2mAWi73@vger.kernel.org, AJvYcCWfTY9rn16Ymt5q3xi1nS9psGebLwoZhhCLmvTt1nY59UuC56LK4aiARE+CZCWdTZJO4UC/QS1fneoc@vger.kernel.org, AJvYcCXqNRIRgVwSmeKEtf1bvYqGnoZCvFvcjoGIerMTAI4QQejjy6ryoQrj9q5mlSBAWJtFHqyJmkRiFKDRA9I8@vger.kernel.org X-Gm-Message-State: AOJu0YyZ62c2Lj5JMDEW9HwE2m/RR75MtxJd7r2xB+QPACx1rxJ6t/wZ ALtReharcmdbyooW66uWmwbXKJq9bllr47IhKoUEdrD0iIBh/uZ1 X-Google-Smtp-Source: AGHT+IG3yZ7QFdXMQgZy4VqVWPZpaI1h3p/gTRs7/ehSdIlWEGnDqq5cSNi9pfS2CM4e0Baco2WIzg== X-Received: by 2002:a05:6820:80f:b0:5eb:db1c:a85d with SMTP id 006d021491bc7-5ebee8e6619mr5987300eaf.4.1729843645926; Fri, 25 Oct 2024 01:07:25 -0700 (PDT) Received: from localhost.localdomain ([122.8.183.87]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5ec1843c72csm157328eaf.1.2024.10.25.01.07.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Oct 2024 01:07:24 -0700 (PDT) From: Chen Wang To: ukleinek@kernel.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, unicorn_wang@outlook.com, inochiama@outlook.com, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org, linux-riscv@lists.infradead.org, chao.wei@sophgo.com, haijiao.liu@sophgo.com, xiaoguang.xing@sophgo.com, chunzhi.lin@sophgo.com Cc: Krzysztof Kozlowski Subject: [PATCH v5 1/3] dt-bindings: pwm: sophgo: add PWM controller for SG2042 Date: Fri, 25 Oct 2024 16:07:16 +0800 Message-Id: <9f9a31a2a19c2743aea36c479b0dc32da0fec629.1729843087.git.unicorn_wang@outlook.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: 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 Content-Type: text/plain; charset="utf-8" From: Chen Wang Sophgo SG2042 contains a PWM controller, which has 4 channels and can generate PWM waveforms output. Signed-off-by: Chen Wang Reviewed-by: Krzysztof Kozlowski --- .../bindings/pwm/sophgo,sg2042-pwm.yaml | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Documentation/devicetree/bindings/pwm/sophgo,sg2042-pwm= .yaml diff --git a/Documentation/devicetree/bindings/pwm/sophgo,sg2042-pwm.yaml b= /Documentation/devicetree/bindings/pwm/sophgo,sg2042-pwm.yaml new file mode 100644 index 000000000000..5dea41fa4c44 --- /dev/null +++ b/Documentation/devicetree/bindings/pwm/sophgo,sg2042-pwm.yaml @@ -0,0 +1,58 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/pwm/sophgo,sg2042-pwm.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Sophgo SG2042 PWM controller + +maintainers: + - Chen Wang + +description: + This controller contains 4 channels which can generate PWM waveforms. + +allOf: + - $ref: pwm.yaml# + +properties: + compatible: + const: sophgo,sg2042-pwm + + reg: + maxItems: 1 + + clocks: + maxItems: 1 + + clock-names: + items: + - const: apb + + resets: + maxItems: 1 + + "#pwm-cells": + const: 2 + +required: + - compatible + - reg + - clocks + - clock-names + - resets + +unevaluatedProperties: false + +examples: + - | + #include + + pwm@7f006000 { + compatible =3D "sophgo,sg2042-pwm"; + reg =3D <0x7f006000 0x1000>; + #pwm-cells =3D <2>; + clocks =3D <&clock 67>; + clock-names =3D "apb"; + resets =3D <&rstgen RST_PWM>; + }; --=20 2.34.1 From nobody Mon Nov 25 17:52:37 2024 Received: from mail-ot1-f50.google.com (mail-ot1-f50.google.com [209.85.210.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9083F1CB9F6; Fri, 25 Oct 2024 08:07:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729843668; cv=none; b=fmRipI0noZOV0jT6o3YPqwEb8F0ALCH4yJjkKbF4Lb1xU6byWL/FTO6vAuhJ9Is34cAasJLvUKqU9lHeqKdcjpTYjrqyfvAiWqMl80CA4nWwp7ErGwFCg7QNc+PbMH0PTi2xow6LI22s1u3sfQLPGwOwFFHSRF5IgEgZYSrVZFw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729843668; c=relaxed/simple; bh=MgMVj0z5F3MCbUi78irJrYSmPQSSOsTHrR+RLkj9tZA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EwHHnLCN50qzq7UHdFXqRMsL3n0Ix1OvscXDexW6d02q6cYw2SEd1F1ozPMKg9PWAzZnnb9+nfMelsM3hzhS4nyMq/l1j3XRbI0odHMVTL+42m22yUlp8LawI+8hJMyWqSHIIaMQ9vFGqInkA1U9gjDYjrPib14cVH1VJo69Y84= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gI9LQFjN; arc=none smtp.client-ip=209.85.210.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gI9LQFjN" Received: by mail-ot1-f50.google.com with SMTP id 46e09a7af769-71822fabf4fso1027805a34.2; Fri, 25 Oct 2024 01:07:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729843663; x=1730448463; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qk4+oNf4uw3HZrJG9g8mUMeRJ62EgsnaH5WrBFOGN5k=; b=gI9LQFjNa4TUinZVRxJlcvZO8aS6gVzeTMJZFZmxjmTXsjytMYqaCWCgmuuFvf9OYi SmhcKHVw6bTC6QoTqa+XlNxczC9170Ja9A5i2PUuefdykXlkAIxF3eeAy5+1hNgot8+i /4AmmoVT7jCuwvvNLHBFu/LQfJIiHQMALM9kvhMN8Xvp7e6a9xxgt6i8Y92dQ8rN6qqM zwLug/ulQb4AZPNOoGdVylhB55CiQdtjnLLMKi+5OnkdyD57cz7ZmN65SmHcr0Z2o70s B6NlJmJsPh9D7Ki8VVc/sNFZE0q/WprAabR4O3yGqcZfL2u3YxtQ5NaSinBvm4og/D7s 3ooQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729843663; x=1730448463; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qk4+oNf4uw3HZrJG9g8mUMeRJ62EgsnaH5WrBFOGN5k=; b=Kyn7V86J465DdRTDBujFHcGNp1Isk1ue8+DKQVDfbJfVXMbXEdwNR6GLbFFGcctQnJ XCB0uhNq7giSLv57s3UQaof6ZAmF9vv7172S5jOPrfw4UQ41Xl7hXBXb1zo8dFiqMs6Z Nqz5Nfk4o0T6IcKUwierJBjmH7nTv1TJA4ZP0lyQcok2O6Lkk7khDDciQwViCtCnqluX +kqmbMYkpTARvL4dcKv2hSgUNL3kfHAuxIXc8lzJNSb66CTQT0JCjwoXOEvlScDdFSjs kZCZi/aQZWjnrhlWsOiCl42pB6PL6Reu2UQ1+udKAsT0DXIjgZV0rr3Pf1GDq0/vk18A 6o8Q== X-Forwarded-Encrypted: i=1; AJvYcCV4ReNvCe0PvOfScbYtdp+ygAh9fRX242+P8h4iSZ6R0p6DzHzv3obiEdIrHyPmQiy8pCTWtNrNdpvR@vger.kernel.org, AJvYcCVJtsqFrDENKFF7RzMXpsdZpOQgXuslUOJgRM7R26m+gKbDTX7KUz+sRvQUVhf//Go2Uecp9EmfXE6B@vger.kernel.org, AJvYcCXgBc4t5KEWarrXusaBPF53u8V2YpacoLMRlEBeBs18/VjDzMY6v7LSDG8kuMUbKsyiqB9HqFFjP2AubhM8@vger.kernel.org X-Gm-Message-State: AOJu0Yxkc/cbqN1msEqY9hcWoBp3UxYcm3RDAat7njTrcsswyqJrAK+J RjMTfXcQbTRZR/D4J422seD7zYCjDVsTCNJweikdN1xqWI2NSaeuKVsudOB1 X-Google-Smtp-Source: AGHT+IH+vWSdxMkvCKRIZuC2GKv3UoSnKoKHfkLlI5WyuqKh6iHekjEfItWsFw9Z8d1tzw+HTP9T7A== X-Received: by 2002:a05:6830:6d15:b0:718:119:ee15 with SMTP id 46e09a7af769-718594231cdmr4563701a34.10.1729843663506; Fri, 25 Oct 2024 01:07:43 -0700 (PDT) Received: from localhost.localdomain ([122.8.183.87]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5ec187386a2sm154702eaf.46.2024.10.25.01.07.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Oct 2024 01:07:42 -0700 (PDT) From: Chen Wang To: ukleinek@kernel.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, unicorn_wang@outlook.com, inochiama@outlook.com, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org, linux-riscv@lists.infradead.org, chao.wei@sophgo.com, haijiao.liu@sophgo.com, xiaoguang.xing@sophgo.com, chunzhi.lin@sophgo.com Cc: Sean Young Subject: [PATCH v5 2/3] pwm: sophgo: add driver for Sophgo SG2042 PWM Date: Fri, 25 Oct 2024 16:07:35 +0800 Message-Id: <4abe3cd4a870fc15b5e54a48eb4554a7419a4ef8.1729843087.git.unicorn_wang@outlook.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: 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 Content-Type: text/plain; charset="utf-8" From: Chen Wang Add a PWM driver for PWM controller in Sophgo SG2042 SoC. Signed-off-by: Chen Wang Signed-off-by: Sean Young --- drivers/pwm/Kconfig | 10 ++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-sophgo-sg2042.c | 194 ++++++++++++++++++++++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 drivers/pwm/pwm-sophgo-sg2042.c diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig index 0915c1e7df16..ec85f3895936 100644 --- a/drivers/pwm/Kconfig +++ b/drivers/pwm/Kconfig @@ -584,6 +584,16 @@ config PWM_SL28CPLD To compile this driver as a module, choose M here: the module will be called pwm-sl28cpld. =20 +config PWM_SOPHGO_SG2042 + tristate "Sophgo SG2042 PWM support" + depends on ARCH_SOPHGO || COMPILE_TEST + help + PWM driver for the PWM controller on Sophgo SG2042 SoC. The PWM + controller supports outputing 4 channels of PWM waveforms. + + To compile this driver as a module, choose M here: the module + will be called pwm_sophgo_sg2042. + config PWM_SPEAR tristate "STMicroelectronics SPEAr PWM support" depends on PLAT_SPEAR || COMPILE_TEST diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile index 9081e0c0e9e0..539e0def3f82 100644 --- a/drivers/pwm/Makefile +++ b/drivers/pwm/Makefile @@ -53,6 +53,7 @@ obj-$(CONFIG_PWM_RZ_MTU3) +=3D pwm-rz-mtu3.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 +obj-$(CONFIG_PWM_SOPHGO_SG2042) +=3D pwm-sophgo-sg2042.o obj-$(CONFIG_PWM_SPEAR) +=3D pwm-spear.o obj-$(CONFIG_PWM_SPRD) +=3D pwm-sprd.o obj-$(CONFIG_PWM_STI) +=3D pwm-sti.o diff --git a/drivers/pwm/pwm-sophgo-sg2042.c b/drivers/pwm/pwm-sophgo-sg204= 2.c new file mode 100644 index 000000000000..a3d12505e4aa --- /dev/null +++ b/drivers/pwm/pwm-sophgo-sg2042.c @@ -0,0 +1,194 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Sophgo SG2042 PWM Controller Driver + * + * Copyright (C) 2024 Sophgo Technology Inc. + * Copyright (C) 2024 Chen Wang + * + * Limitations: + * - After reset, the output of the PWM channel is always high. + * The value of HLPERIOD/PERIOD is 0. + * - When HLPERIOD or PERIOD is reconfigured, PWM will start to + * output waveforms with the new configuration after completing + * the running period. + * - When PERIOD and HLPERIOD is set to 0, the PWM wave output will + * be stopped and the output is pulled to high. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +/* + * Offset RegisterName + * 0x0000 HLPERIOD0 + * 0x0004 PERIOD0 + * 0x0008 HLPERIOD1 + * 0x000C PERIOD1 + * 0x0010 HLPERIOD2 + * 0x0014 PERIOD2 + * 0x0018 HLPERIOD3 + * 0x001C PERIOD3 + * Four groups and every group is composed of HLPERIOD & PERIOD + */ +#define SG2042_HLPERIOD(chan) ((chan) * 8 + 0) +#define SG2042_PERIOD(chan) ((chan) * 8 + 4) + +#define SG2042_PWM_CHANNELNUM 4 + +/** + * struct sg2042_pwm_ddata - private driver data + * @base: base address of mapped PWM registers + * @clk_rate_hz: rate of base clock in HZ + */ +struct sg2042_pwm_ddata { + void __iomem *base; + unsigned long clk_rate_hz; +}; + +static void pwm_sg2042_config(void __iomem *base, unsigned int chan, u32 p= eriod, u32 hlperiod) +{ + writel(period, base + SG2042_PERIOD(chan)); + writel(hlperiod, base + SG2042_HLPERIOD(chan)); +} + +static int pwm_sg2042_apply(struct pwm_chip *chip, struct pwm_device *pwm, + const struct pwm_state *state) +{ + struct sg2042_pwm_ddata *ddata =3D pwmchip_get_drvdata(chip); + u32 hlperiod; + u32 period; + + if (state->polarity =3D=3D PWM_POLARITY_INVERSED) + return -EINVAL; + + if (!state->enabled) { + pwm_sg2042_config(ddata->base, pwm->hwpwm, 0, 0); + return 0; + } + + /* + * Period of High level (duty_cycle) =3D HLPERIOD x Period_clk + * Period of One Cycle (period) =3D PERIOD x Period_clk + */ + period =3D min(mul_u64_u64_div_u64(ddata->clk_rate_hz, state->period, NSE= C_PER_SEC), U32_MAX); + hlperiod =3D min(mul_u64_u64_div_u64(ddata->clk_rate_hz, state->duty_cycl= e, NSEC_PER_SEC), U32_MAX); + + if (hlperiod > period) { + dev_err(pwmchip_parent(chip), "period < hlperiod, failed to apply curren= t setting\n"); + return -EINVAL; + } + + dev_dbg(pwmchip_parent(chip), "chan[%u]: period=3D%u, hlperiod=3D%u\n", + pwm->hwpwm, period, hlperiod); + + pwm_sg2042_config(ddata->base, pwm->hwpwm, period, hlperiod); + + return 0; +} + +static int pwm_sg2042_get_state(struct pwm_chip *chip, struct pwm_device *= pwm, + struct pwm_state *state) +{ + struct sg2042_pwm_ddata *ddata =3D pwmchip_get_drvdata(chip); + unsigned int chan =3D pwm->hwpwm; + u32 hlperiod; + u32 period; + + period =3D readl(ddata->base + SG2042_PERIOD(chan)); + hlperiod =3D readl(ddata->base + SG2042_HLPERIOD(chan)); + + if (!period && !hlperiod) + state->enabled =3D false; + else + state->enabled =3D true; + + state->period =3D DIV_ROUND_UP_ULL((u64)period * NSEC_PER_SEC, ddata->clk= _rate_hz); + state->duty_cycle =3D DIV_ROUND_UP_ULL((u64)hlperiod * NSEC_PER_SEC, ddat= a->clk_rate_hz); + + state->polarity =3D PWM_POLARITY_NORMAL; + + return 0; +} + +static const struct pwm_ops pwm_sg2042_ops =3D { + .apply =3D pwm_sg2042_apply, + .get_state =3D pwm_sg2042_get_state, +}; + +static const struct of_device_id sg2042_pwm_ids[] =3D { + { .compatible =3D "sophgo,sg2042-pwm" }, + { } +}; +MODULE_DEVICE_TABLE(of, sg2042_pwm_ids); + +static int pwm_sg2042_probe(struct platform_device *pdev) +{ + struct device *dev =3D &pdev->dev; + struct sg2042_pwm_ddata *ddata; + struct reset_control *rst; + struct pwm_chip *chip; + struct clk *clk; + int ret; + + chip =3D devm_pwmchip_alloc(dev, SG2042_PWM_CHANNELNUM, sizeof(*ddata)); + if (IS_ERR(chip)) + return PTR_ERR(chip); + ddata =3D pwmchip_get_drvdata(chip); + + ddata->base =3D devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(ddata->base)) + return PTR_ERR(ddata->base); + + clk =3D devm_clk_get_enabled(dev, "apb"); + if (IS_ERR(clk)) + return dev_err_probe(dev, PTR_ERR(clk), "failed to get base clk\n"); + + ret =3D devm_clk_rate_exclusive_get(dev, clk); + if (ret) + return dev_err_probe(dev, ret, "failed to get exclusive rate\n"); + + ddata->clk_rate_hz =3D clk_get_rate(clk); + if (!ddata->clk_rate_hz || ddata->clk_rate_hz > NSEC_PER_SEC) + return dev_err_probe(dev, -EINVAL, + "Invalid clock rate: %lu\n", ddata->clk_rate_hz); + + rst =3D devm_reset_control_get_optional_shared(dev, NULL); + if (IS_ERR(rst)) + return dev_err_probe(dev, PTR_ERR(rst), "failed to get reset\n"); + + /* Deassert reset */ + ret =3D reset_control_deassert(rst); + if (ret) + return dev_err_probe(dev, ret, "failed to deassert\n"); + + chip->ops =3D &pwm_sg2042_ops; + chip->atomic =3D true; + + ret =3D devm_pwmchip_add(dev, chip); + if (ret < 0) { + reset_control_assert(rst); + return dev_err_probe(dev, ret, "failed to register PWM chip\n"); + } + + return 0; +} + +static struct platform_driver pwm_sg2042_driver =3D { + .driver =3D { + .name =3D "sg2042-pwm", + .of_match_table =3D sg2042_pwm_ids, + }, + .probe =3D pwm_sg2042_probe, +}; +module_platform_driver(pwm_sg2042_driver); + +MODULE_AUTHOR("Chen Wang"); +MODULE_DESCRIPTION("Sophgo SG2042 PWM driver"); +MODULE_LICENSE("GPL"); --=20 2.34.1 From nobody Mon Nov 25 17:52:37 2024 Received: from mail-ot1-f45.google.com (mail-ot1-f45.google.com [209.85.210.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C419F1CFEB1; Fri, 25 Oct 2024 08:08:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729843682; cv=none; b=iBfNRS0QSdGnP2FHB58G9aAjf65aJfGiq7hpG3dvkBFFtkFyHKVPakTjuRd4shjRTDnDFSQOEnT4o4IOjNtaVg1ebMrKDGyZNVBASm3o+QzBiTDU2BgQ7mJU3w+OmAQTYVZe7+58GIwDl8pwM1j8enFu1gQtAo+2IwEkrBnmDXA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729843682; c=relaxed/simple; bh=O8pyvZDag9iaANQFXZ/6mrX2jzrwvBo2OjNwZJ++M5M=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=leN6mo3j/gydK3bSpm0s1Esi7k2mUrB2JsX8eycWvyb3Cx1tzACMFOR+94WlkN6/GI1zmVp5gYRQ73CGEAq/IgIIyO7qdliE64BfyoKJMbpX6Ynmr8ztno6nIN2qr/wH/eBtwhS+tgR66HZPy25V6qsaKs5vutFHrv7u+ORC5kg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JnpMC4X8; arc=none smtp.client-ip=209.85.210.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JnpMC4X8" Received: by mail-ot1-f45.google.com with SMTP id 46e09a7af769-7093997dffdso722873a34.2; Fri, 25 Oct 2024 01:08:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729843680; x=1730448480; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=rN5eVpbvHpbGL4WpynMeSKENdrHYe60OqvDbvd5Xiq4=; b=JnpMC4X8Zo+mJvC94C/wBFBnWIV/P1Q6LCRK4WlGmWRTJLiE82oJXUAzTsGbNCGNwn UkuG9Fs3r4RKKm8n2pZhLqkBgvtdjSo+rcyocV279lbihgZXpVjjUS0II+qiT8y1Musq sCrPDYXnJpWOIWyEP4XI7M+gSGcQTmfd8BB82K8hr0O9xiLJxRwC/JlQiZ5ps95hWsYL 1M7eEOddA/GHdaPztcmSsStPuN9jnva86RnV2Pu6vD7k5X1yR2Z52A0xM+2RusKNMtJ7 1kd4/jl1BUzZknT0+i9+uxEHJFa7vy4zxEq6zju+LpDGkFOjt8kGJHsGZfSbCS5rnsDb KBMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729843680; x=1730448480; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rN5eVpbvHpbGL4WpynMeSKENdrHYe60OqvDbvd5Xiq4=; b=YK9LNcqeGhw3iaWoi1UuLsnLfiBgKMNb1jpXamfpkpVoZeud1XAg4+cAEo0IQPA7X5 6ujI77pZ8Ee1S5nMDsSW7Eo1nBxmMWJgP0MZnBD7N2bG/lAcUwKvVHEdZd09Fx5zcfGF q7T0L1iecCFR+iCvzYF5X5dYaNysWJMYeXrfRV0rRPzJIKd6b4R150L047y7gNJaYEPH nIDchng/AD9QIbaoKI27TXhRp6ixCu5XadgVdBjgHThSHn+GT2igZ8xi4HK3mllTiDuL 5xrO/sYJwJGxOe5OmSxckZQ5a3G5rgoVgv+Beuc6HCGqLRHNzzVBT1FJiCQaoC9F31ii FYZw== X-Forwarded-Encrypted: i=1; AJvYcCW7Af8Jxgvg9kXhPpM0fvRA5t5t1mGmED4qFzcBjw+erkWFOjp/nUHSNwyEPVFTmhUon5FuiLAV8ZEtY754@vger.kernel.org, AJvYcCWrL4Jo05Oy8c1oVXfjewD2r8NjyvSM50dh4GZXxJS0YmYXNnAGrVGbJigpYYqouifWcdstJvpu6585@vger.kernel.org, AJvYcCXbB/HotLUOMNgX4nTxsaUzb7BIx+MqPDIiE5Egf3Me3Fk5+UllU1+D7kKozRe5zrXGGl2nA6g1jkkf@vger.kernel.org X-Gm-Message-State: AOJu0Yxfrc8jjfgkQraJh3KQUgVHrYt0v/VOo4s5ZaRzNCpZF2EdsKLF ZBf1yOWIL8UWA1PDIfQIu4HDclq/NXIstp1+cSLK7uDkpj++qmCq X-Google-Smtp-Source: AGHT+IE+aA1kZV0Gkelsvo6Wh/wBlEOQ5MwEkYl2tGn0oofv0qQbf+/aSQ5fFXMTaUtSRKeXwOxgcg== X-Received: by 2002:a05:6830:6f01:b0:718:10ce:c6a7 with SMTP id 46e09a7af769-7184b348e49mr7419539a34.30.1729843679811; Fri, 25 Oct 2024 01:07:59 -0700 (PDT) Received: from localhost.localdomain ([122.8.183.87]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5ec1859b627sm160231eaf.18.2024.10.25.01.07.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Oct 2024 01:07:58 -0700 (PDT) From: Chen Wang To: ukleinek@kernel.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, unicorn_wang@outlook.com, inochiama@outlook.com, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org, linux-riscv@lists.infradead.org, chao.wei@sophgo.com, haijiao.liu@sophgo.com, xiaoguang.xing@sophgo.com, chunzhi.lin@sophgo.com Subject: [PATCH v5 3/3] riscv: sophgo: dts: add pwm controller for SG2042 SoC Date: Fri, 25 Oct 2024 16:07:51 +0800 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: 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 Content-Type: text/plain; charset="utf-8" From: Chen Wang SG2042 has one PWM controller, which has 4 pwm output channels. Signed-off-by: Chen Wang --- arch/riscv/boot/dts/sophgo/sg2042.dtsi | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/riscv/boot/dts/sophgo/sg2042.dtsi b/arch/riscv/boot/dts/s= ophgo/sg2042.dtsi index 4e5fa6591623..cc33115fcd8c 100644 --- a/arch/riscv/boot/dts/sophgo/sg2042.dtsi +++ b/arch/riscv/boot/dts/sophgo/sg2042.dtsi @@ -165,6 +165,15 @@ port2a: gpio-controller@0 { }; }; =20 + pwm: pwm@703000c000 { + compatible =3D "sophgo,sg2042-pwm"; + reg =3D <0x70 0x3000c000 0x0 0x20>; + #pwm-cells =3D <2>; + clocks =3D <&clkgen GATE_CLK_APB_PWM>; + clock-names =3D "apb"; + resets =3D <&rstgen RST_PWM>; + }; + pllclk: clock-controller@70300100c0 { compatible =3D "sophgo,sg2042-pll"; reg =3D <0x70 0x300100c0 0x0 0x40>; --=20 2.34.1