From patchwork Wed Dec 4 03:16:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Wang X-Patchwork-Id: 13893183 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BC6F0E7716B for ; Wed, 4 Dec 2024 03:16:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=9GW/E5LvFNAAiI1tFgW/mzVcQDiewBEgxeyC3mSpcLM=; b=e8ytQAZL2PEtvH 59onLG3DRqnWUel+B8OlS6D3o1e7FNnalXWXA1QsdHV5oRHEUvNUUM4o+dwj/xTREDOIg5bJqGgWo P4GBSx4FDKz2FIPgL7oJHTX4EPTf8hPtKC9AQXqeajnm9giLl5os4HTMJ8+rrYkcwxJMAsLKsR7dv KUfG18J+QxGz+ZTkozLo5fXcB3Q3IrpoelamgnJJPz7GmblMj0zrtAnNsrvLMitbiuw9NsP7w6d23 hAIT4ROZN0YIB1d+Z5H5ChV+KahrIzmcvKpTBjzL9JJ4Ruz5KNgwgqwYn0yApF03iqILObMaPjnCX uG7qHeBnUsYzOi7zaVAQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIfsR-0000000BMTu-0Nnm; Wed, 04 Dec 2024 03:16:35 +0000 Received: from mail-oa1-x2a.google.com ([2001:4860:4864:20::2a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIfsO-0000000BMTL-35y6 for linux-riscv@lists.infradead.org; Wed, 04 Dec 2024 03:16:34 +0000 Received: by mail-oa1-x2a.google.com with SMTP id 586e51a60fabf-29e99130ff3so227266fac.1 for ; Tue, 03 Dec 2024 19:16:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733282191; x=1733886991; darn=lists.infradead.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=MXULZdyaVvx/OiMCDze9SUzlIsvxwquwyC2hd5UuXrzaoPyztdaReURCHSwwNrZtp2 W5T1OTubuE7vZvWlzE+WpABuSzEaRgvXIZF7QMoGIJ0OHv7Wy4pHhmnRpUCu10U9U74b N3EnsaBbCjXY3lMJ4TQkCbOpV2RJn4x32MXDOqEm/Nb58HBFKMtia+k4sLICpCgIv5gC UI9q3aMTSnQcHUyXcWNuS7zjZ0AusSgc/wvw0xoZMQ/khJLYGGcZ9QrKsOrAGZX6Jq+a XO+5RCb2ykkR7oQ6D2rfLaHxxwenh8a3y4RlwAqNYfCOXAtcPDvYOS10/iIfcSau2w81 dvag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733282191; x=1733886991; 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=Pa1GgqK8NZi/on26SbVku0uIgcX2XVuOXM0TKJUPp39XVFQ98+vcN0FYADOeuSPfhb fh35n6cB7X7utHu1DPUmnnQHW3sFOLlYSwHqYkVQ4+xU1dgHl3BRCgItUKkAEHVM1fDg T7t+xBY58uOeJVvkL5E7/a/0RZjsBK8KWMNsAbftGVB+86fWnQwchzN6ltPBq+6/cp7W 5NCiJSrQhNezc/7hJwl7SxPBby3FrGjh1+F8Es5xVAu3FRWJOgoJYuvouD4Do4GdhzFC V8rEtJ3Y3goQvB1eLUuanahz+aFMdf1F77Z5FbBxyIui230wSPmwiUxIP8Wi5f5Pr0F/ KTsg== X-Forwarded-Encrypted: i=1; AJvYcCXcPmp01HEf+IYQmgu5Plf1yJ9KK7dBzJxHXbZym24qRZyPUTOrPNO+2Rb5smmpuInBGxKzLQeOVhoiGg==@lists.infradead.org X-Gm-Message-State: AOJu0YyeG11NYnJWsfLTGA3kJ660NQiB0swuZjmrGFvXkIScupNagy+J o09QbQYnnXT3derHeLrVJTTJfCwSb9xN54gGwWpAh7Ky1wGFX7BB X-Gm-Gg: ASbGncuB70veVf1jGgFf7xvJRCX7Nv9YRlRTsSnqaGRfjNg0dMOQ10/3cvh7tE5RdRn t8WB/OvvrXyzizLeoKKMmDArfaLL5YXxU8/ojMdgktHVzQLufEKYtoZ43BRCV8hgMW3L1w0PSbe ogRaBFWCCoSgq6QFiMoXqaO6BK+UiS4pjsw5dHamE3Erc+/hA23t6+Vc/SlmMvjd4ttJ0ncfYcb FJVA+KBut7Vj99wQbYmu8CL+wh/IfOoLaAdomujHkc+PdPz6/nSunaAYo9n X-Google-Smtp-Source: AGHT+IHY5LjkclYMi3XMIUv3pdnbphLNp1wA4vpon3cxectbEX98rvxmO78DG4oypv59/M909N6rlQ== X-Received: by 2002:a05:6870:55ce:b0:29e:7629:1466 with SMTP id 586e51a60fabf-29e76296df1mr5314546fac.7.1733282191529; Tue, 03 Dec 2024 19:16:31 -0800 (PST) Received: from localhost.localdomain ([122.8.183.87]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-71d8691ae5asm2425786a34.12.2024.12.03.19.16.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Dec 2024 19:16:30 -0800 (PST) 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 v6 1/3] dt-bindings: pwm: sophgo: add PWM controller for SG2042 Date: Wed, 4 Dec 2024 11:16:22 +0800 Message-Id: <673f314d78ab467399afc02b96ac730149f19587.1733281657.git.unicorn_wang@outlook.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241203_191632_775369_35529A29 X-CRM114-Status: GOOD ( 11.29 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 = "sophgo,sg2042-pwm"; + reg = <0x7f006000 0x1000>; + #pwm-cells = <2>; + clocks = <&clock 67>; + clock-names = "apb"; + resets = <&rstgen RST_PWM>; + }; From patchwork Wed Dec 4 03:17:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Wang X-Patchwork-Id: 13893184 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 46666E7716C for ; Wed, 4 Dec 2024 03:17:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=9kV2eILOxusTAyH8gNS5wbcJJ6haZoPdj2RhLU3Dexk=; b=jxkTAR1MkyUgL+ PFfys6dlP1DtLOEcbNta2DS2Z/pE9qw1wWNXiVAuTmnw3X5jranpItjd5n4DjzZvZ2svl93mo8gFr t9kp812TrPSB2sG0Y6qGhT+XGs8OrBsLQt/PWx5MOWLNH38pjTrLxxmsgNsbl465g5Q3HLZ+R5o3f eGLqZWu32pZ6LE1ppHKG1HCHrAKNTMj8X7uz9uoS+o28ogX810QQz7ILrFIZLDiZ8eQaAQvpqtugi VtM+NdppMXo4ScxLQHBGH4Dm4ld7ea4JacwtifqQHB3e9VS39oF/sMIY9y4phRYEkt9v63qdG7xsK iIJTXyMIhLvVfbzhux3Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIftK-0000000BMey-2wi4; Wed, 04 Dec 2024 03:17:30 +0000 Received: from mail-oa1-x33.google.com ([2001:4860:4864:20::33]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIftI-0000000BMeK-2qzc for linux-riscv@lists.infradead.org; Wed, 04 Dec 2024 03:17:29 +0000 Received: by mail-oa1-x33.google.com with SMTP id 586e51a60fabf-29e644b3f6fso1736737fac.2 for ; Tue, 03 Dec 2024 19:17:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733282247; x=1733887047; darn=lists.infradead.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=EcAEYFyb9HK7XuoPQLjfwngI1vWA6m7rzYrLyYDDA71mUcma6KJBzndri0w+XUgnYA T17DZs0Xusve4b/0xi0XLVS3Zb66BD6qbO2gco56GM4fAF1+sJ4OAPEWcIYVnw/fZY1I XiJns1fsjHLNetbDYhlYVbe0/rzB9fjUbrVknCjnQgtv8qgcvCZThI+TmRx0rkRRxznP w+DdcP4VBnt25Gdw/adspJQDSgN41RhLV1Ls13T/pB3RCBGxwBVURhOaQwEJZtYHRupv iouEgpubR+prIfBQBdxdc3rnq+Mo6032kreLMDIEq0QKpGTHBrr0nqjMDphSCxQaEshe Mc9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733282247; x=1733887047; 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=aXE5gletP/QwGWbZOhhW5pEhGEKBlsw+YBpaRSiH//SvP0V60bZ2ADnwbzQokC3Ttl ag6RbcJuPFxIJAtoK9Hhqpq+lSsmxbJFemmDtcX0Dy3CW9C8ekm3YivcMqBCU98hlnhU SZO/GqRfNH+uU4rtXAg++SDEl4E1uARznLig3KmQnVDgmfIFcfJjtkZhTYKHfvmzcpGn G350tzBgAy3shvo3m6qeqT6DfLxaMA9m57fpBFvd8/YtlpwC6St4WCXLPAUQk9jh3wnc QFsBHvG965JraFGqPRmq8ZZ9/TQgs+7gZFOElOj6Uz/yelw1JPGP9Z8xg1VZRKUVNKKR ksiA== X-Forwarded-Encrypted: i=1; AJvYcCXvg1IO3RpWoAzaCjJPfd/HGwB1BjWMw7wZC7NbkM1tYug+ZYVOv7cMDXVW4AoYfECt5SMTKpm5wZPqKQ==@lists.infradead.org X-Gm-Message-State: AOJu0YzkkNRMzK+S6ksOqqShIetoJhts167NY/VDVNoSZ1jrcM4tdDOz Lb0ISjTzgLpD60rYDZ1qsFYXZmeYe+lDPNy4tzn7GtY9CfFg7oTt X-Gm-Gg: ASbGnctMdTc7q2WBd3TWsPiay6bC7lPXyD3bH6QjqUWhEizMVbyQf/muJlvHnVNJF9c okFH5RgCVOgfuo6Rj8hYyPX01CbkGl97XrNSGlasvcV7Rh1lcTP2B/QmFRf+EBwFCWIYGqB9nAq NwvAapotDfJQWEJ3WwXRxZtvVaK9uiADkSqs1idBOBWh+Sh/gx5exfq6pbdMbl3UD+2ntk4ttFg 8VEyuA5heNmNxkRgent2aL/jlyTvI50lN4jDwbev220AGslBf+htOPaCCPa X-Google-Smtp-Source: AGHT+IE2qIZh2KttRDHnpTLqWXMk+OulkhGE/kBos3m80jRmNAZAn0GOlVw6E36MfEKk2vQUZhuWYw== X-Received: by 2002:a05:6871:5315:b0:29e:5df2:3e50 with SMTP id 586e51a60fabf-29e8868881amr6023388fac.15.1733282247578; Tue, 03 Dec 2024 19:17:27 -0800 (PST) Received: from localhost.localdomain ([122.8.183.87]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-29e6ef90f5dsm1429223fac.51.2024.12.03.19.17.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Dec 2024 19:17:26 -0800 (PST) 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 v6 2/3] pwm: sophgo: add driver for Sophgo SG2042 PWM Date: Wed, 4 Dec 2024 11:17:18 +0800 Message-Id: <4a7db3ecac9ac2b00c063360334834d6656672e3.1733281657.git.unicorn_wang@outlook.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241203_191728_721479_43A657EF X-CRM114-Status: GOOD ( 26.91 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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. +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) += pwm-rz-mtu3.o obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o obj-$(CONFIG_PWM_SIFIVE) += pwm-sifive.o obj-$(CONFIG_PWM_SL28CPLD) += pwm-sl28cpld.o +obj-$(CONFIG_PWM_SOPHGO_SG2042) += pwm-sophgo-sg2042.o obj-$(CONFIG_PWM_SPEAR) += pwm-spear.o obj-$(CONFIG_PWM_SPRD) += pwm-sprd.o obj-$(CONFIG_PWM_STI) += pwm-sti.o diff --git a/drivers/pwm/pwm-sophgo-sg2042.c b/drivers/pwm/pwm-sophgo-sg2042.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 period, 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 = pwmchip_get_drvdata(chip); + u32 hlperiod; + u32 period; + + if (state->polarity == 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) = HLPERIOD x Period_clk + * Period of One Cycle (period) = PERIOD x Period_clk + */ + period = min(mul_u64_u64_div_u64(ddata->clk_rate_hz, state->period, NSEC_PER_SEC), U32_MAX); + hlperiod = min(mul_u64_u64_div_u64(ddata->clk_rate_hz, state->duty_cycle, NSEC_PER_SEC), U32_MAX); + + if (hlperiod > period) { + dev_err(pwmchip_parent(chip), "period < hlperiod, failed to apply current setting\n"); + return -EINVAL; + } + + dev_dbg(pwmchip_parent(chip), "chan[%u]: period=%u, hlperiod=%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 = pwmchip_get_drvdata(chip); + unsigned int chan = pwm->hwpwm; + u32 hlperiod; + u32 period; + + period = readl(ddata->base + SG2042_PERIOD(chan)); + hlperiod = readl(ddata->base + SG2042_HLPERIOD(chan)); + + if (!period && !hlperiod) + state->enabled = false; + else + state->enabled = true; + + state->period = DIV_ROUND_UP_ULL((u64)period * NSEC_PER_SEC, ddata->clk_rate_hz); + state->duty_cycle = DIV_ROUND_UP_ULL((u64)hlperiod * NSEC_PER_SEC, ddata->clk_rate_hz); + + state->polarity = PWM_POLARITY_NORMAL; + + return 0; +} + +static const struct pwm_ops pwm_sg2042_ops = { + .apply = pwm_sg2042_apply, + .get_state = pwm_sg2042_get_state, +}; + +static const struct of_device_id sg2042_pwm_ids[] = { + { .compatible = "sophgo,sg2042-pwm" }, + { } +}; +MODULE_DEVICE_TABLE(of, sg2042_pwm_ids); + +static int pwm_sg2042_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct sg2042_pwm_ddata *ddata; + struct reset_control *rst; + struct pwm_chip *chip; + struct clk *clk; + int ret; + + chip = devm_pwmchip_alloc(dev, SG2042_PWM_CHANNELNUM, sizeof(*ddata)); + if (IS_ERR(chip)) + return PTR_ERR(chip); + ddata = pwmchip_get_drvdata(chip); + + ddata->base = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(ddata->base)) + return PTR_ERR(ddata->base); + + clk = 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 = 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 = 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 = 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 = reset_control_deassert(rst); + if (ret) + return dev_err_probe(dev, ret, "failed to deassert\n"); + + chip->ops = &pwm_sg2042_ops; + chip->atomic = true; + + ret = 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 = { + .driver = { + .name = "sg2042-pwm", + .of_match_table = sg2042_pwm_ids, + }, + .probe = pwm_sg2042_probe, +}; +module_platform_driver(pwm_sg2042_driver); + +MODULE_AUTHOR("Chen Wang"); +MODULE_DESCRIPTION("Sophgo SG2042 PWM driver"); +MODULE_LICENSE("GPL"); From patchwork Wed Dec 4 03:17:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Wang X-Patchwork-Id: 13893185 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1C42EE7716B for ; Wed, 4 Dec 2024 03:18:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=3FnkNQIbjFmjBR7N2WCBfsW9ag94FD8pSxpT5aQwSCU=; b=0RBUFWNdS8EaJH i+8iw9LtSDOll9tQvaZYJD/9NXHeNaecfg/w0soTxK+7v4Cox67IQW8WuZFUqEiXdPJ8129FYvfMy D3tg8Z8A13PXT0aywdJ+RB7lwSmJRK0M5x2KP/VBLl3NmC3alRFpj0m1RN7i0G9PvAb3nx0JqLlRg WTxLfKrZk/syC6Nt2cxec8EYpOOODKdW2iP0KUW8N4HDIfih6uS8XI45542RcirSTCTFImLQIE3Uw dDMm9EMY8i4J76ilBFrIe4dhI9bEoXW8RqSmBwN7Wf2+JDHNPZDWWUi893Km7HgOdfcF65SVXhgaU +OOfVBcrP+0IVd0U/J+g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIftl-0000000BMlq-192l; Wed, 04 Dec 2024 03:17:57 +0000 Received: from mail-oa1-x2a.google.com ([2001:4860:4864:20::2a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIfti-0000000BMlF-1As3 for linux-riscv@lists.infradead.org; Wed, 04 Dec 2024 03:17:55 +0000 Received: by mail-oa1-x2a.google.com with SMTP id 586e51a60fabf-2967af48248so3471988fac.2 for ; Tue, 03 Dec 2024 19:17:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733282273; x=1733887073; darn=lists.infradead.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=mixRlBZnAzl6+RGD+SnJtrTgxRDsBgCOJYnL66Xe1tk=; b=V+UQWigT+hkW0KyD4XXq1wUaDwLVru3UCn/MRdaFsk/6xNlOsgJLG9OEfS6W1s0R6N 0dQLBgc46bR4k4UGaM5izPQJMCxJiJrcU7LPENQ0h4oPsBMLu57wM3DSuAMyN8XGLajw vL73xkD8fd+ax6IZVRVl7jvClPjfyTwx4zYyYtjXCkWG+1EHFtjMB/7F/UWF6pI0UzEE QAovKiAOfddqU8nisYAXKu432qEydqwIssMMToZxNCrMxvolLV6hy2bkj7JsvhWpBMxI bkiWGZk1V5E1nz24sh4KKveODlm010zsBwNcYh2mTxb3VadUV+jpnOoVUz97heZd37yi ZOVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733282273; x=1733887073; 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=mixRlBZnAzl6+RGD+SnJtrTgxRDsBgCOJYnL66Xe1tk=; b=wRM8kngnUwKv03tmaNqF0Ho4ZNis9VLDvwrrWTzNHVStJYejyRXShXCSnpIeWdsrTQ zitqbpgqiygNyRxx3NTbsJJMpCj4f5jsww/7An5/Qt6fopA22AfrUtDZQe5NMLBqaMWM 1KQHzKNhFXJ+rSMHmJ1Ovi0OZSauf13oa7nrdkVC0bxvXMNl+1u24YY+M/0ntaVO+ELY Lfdscaw5WGYn5rMJ3htfvuqjWGXSzlsBFgAkHa6lEypG1Lbtd3DjpNjirc3zxYkLyh3Z 96Scu20OE4Oq8iXJ+xZ2UC2hLgMpHVyGx0D2KLcDz11DOgjU9hMyJ3TVQZ36aiTd5Mu4 fCxg== X-Forwarded-Encrypted: i=1; AJvYcCU/u8eTxsQBjC/2d0Ie0/lZtzwvJZj8ZuZc6+ILHu2Z9m/NeqehJsbbYjEUXU3GTBEwudd9CALW6Fk46g==@lists.infradead.org X-Gm-Message-State: AOJu0YzQqQ6rMLFlqzgQOB8CAZEvUXHtIgjtxnlEPMeca8YkChwaEBMu dWH5dzKJmyEkVBaIbarXgt5yqL+E+XZLYlSiHW4edXxtWDjsC6d+HS/zSMA47IA= X-Gm-Gg: ASbGncv6sthjlVrTnmVYoNBj5cqZTbwbrxd6Nc7n7RYc/17I+nLe2nKXpnlDkWPpI33 Mc72pMDp0wnwn85zKkdWUKEqrXytvnVozOf0ZbCJzHAX0OPjSO21GPJmfqRzjTfqZCGK35n4sF1 dg/chE7AF0rRQmpYPc/feflfyDugho7YFKkwWtyOG0U4/4OG1gLiaaTbCYv+FsY7+Hya/I3Rh69 ylkifLGytnSzHel9wZubiefA7NwOGeJKFvjlbUcLEA74H0o9qHrymZ5p9Qf X-Google-Smtp-Source: AGHT+IGAAZq0BZqx+M5fXCuGO93303qiSaJYO7mY7gecCr/3SuC0Sy1KChSb3UAIulwKdXP909PnVg== X-Received: by 2002:a05:6870:3d8d:b0:29e:3eff:dea with SMTP id 586e51a60fabf-29e9b08c0ebmr1787227fac.8.1733282273522; Tue, 03 Dec 2024 19:17:53 -0800 (PST) Received: from localhost.localdomain ([122.8.183.87]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-29de8f31cefsm4185404fac.2.2024.12.03.19.17.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Dec 2024 19:17:52 -0800 (PST) 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 v6 3/3] riscv: sophgo: dts: add pwm controller for SG2042 SoC Date: Wed, 4 Dec 2024 11:17:45 +0800 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241203_191754_318619_FF982C15 X-CRM114-Status: UNSURE ( 8.42 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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/sophgo/sg2042.dtsi index e62ac51ac55a..77dd65d79946 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 { }; }; + pwm: pwm@703000c000 { + compatible = "sophgo,sg2042-pwm"; + reg = <0x70 0x3000c000 0x0 0x20>; + #pwm-cells = <2>; + clocks = <&clkgen GATE_CLK_APB_PWM>; + clock-names = "apb"; + resets = <&rstgen RST_PWM>; + }; + pllclk: clock-controller@70300100c0 { compatible = "sophgo,sg2042-pll"; reg = <0x70 0x300100c0 0x0 0x40>;