From patchwork Thu Jun 15 14:43:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aleksandr Shubin X-Patchwork-Id: 13281368 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 F0CC9EB64D9 for ; Thu, 15 Jun 2023 14:45: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: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=6n1UAVOw0dCzmctIOnsYhmFu/PYw0hQ5hPctNFcOOqc=; b=oNToURF1Nt0cvv nwO7/3IQYk4UTFAVbM6f6nVsgb9iAeGhFqW2qIAH3pcVKG2KHcQB6ndvv8Px5Uaq+sbctW2JA7xso /Rka0kAHEFGnIQhD4uXCxOM82JcJSjk6EGpk5BijedPoB+bhNNE5nX7UZmDuGi+h+yq99x9RjEEIp 3cSuVW5jOyZzt097oDFyHlUiixHLpBmBp7R2gQmEE03dDaqRq0KKDfRBElnn6DBNdfW3LR/PY47SL pmkKX14NipYqq/5PBk7yrkz5K7B3XaordFJhvztPnaXkPM0K5hY/gzHLAkGFJ3jFQl7n+DISMkF2K MntS+XrHwLHFnWXWdfEw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q9oDa-00FBwl-36; Thu, 15 Jun 2023 14:44:58 +0000 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q9oDW-00FBuj-2v; Thu, 15 Jun 2023 14:44:56 +0000 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-4f62cf9755eso10541386e87.1; Thu, 15 Jun 2023 07:44:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686840292; x=1689432292; 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=01rML8QNhx1meW63X7YOChbwA/I11tVl46mShxzno8c=; b=pvA/+HLk8vgyap9cUCFXxWzeUdNPB9Ek2np+gXBneOchgcRWdGUWKuRKGNmicfLkYe nZmfLMgNIp8JoSlcrocdAVKe9CJ4b+2qcF6UJyPJuVcHgivOfeCZ5ZxczOmUbI0r4v3v nsuRvyqRzSdalni1iFMt1adUhZZ1MyfTVKmQPX/GseW1tjDCDMprOVtMZ9CJQ2IbY81+ Y1zZKj2Yisrge/IDvfWds0v5868fQ+zTWWQpeU13l64gIRQYJTx4+XkSiLxR87/XyA8O juOlTX8a+vBlj3qzIIU4zQJ9UdlN0lJLv69VnMZm9rAy7oHxRNrxZ1tMn7OyG+++oFp/ hOKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686840292; x=1689432292; 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=01rML8QNhx1meW63X7YOChbwA/I11tVl46mShxzno8c=; b=GAC8Rf9LG6NYqF+8NGUqMUk2AlpUxqZJhwh/VX9FWKwLMF3e7BEG0Vl8MJDTjTOuew 3lFmhrcOGxtyi3s/RwjSURBMmtk71ZrJbIycLRxdTm35HUjnEmfsWUFdstVD2gG8cn1v bn1A2kORfW97UTw0L/8PqLnAx/G4cXWbPA6VoK/XKi5prjNZdNQ+eC56bTLpBFc5tK5i qQcKGhici61eRwxCp+GSKwRY5XhZOcEWrlg8SS78Si61e92PqhMB/WsXu5M4cDiUzd9/ Z7+R+PygiAG7kUc98g1dVQ9teBmGNg49bpOgDlAx2rglsZSexwoCNIosVFhiPDRkTwof Pxwg== X-Gm-Message-State: AC+VfDxGzN5RgbDwj584+uFXMl4O2oo+1l8Bfgzvg4XBrnspZ/UfjyWc nmxEMBeELDTeLcMWQ0IX/A== X-Google-Smtp-Source: ACHHUZ6/hm0iRAbwO9RB4MJBdUYxGo5Dp355lzxZq305nEDV93dKD4eDiVGsuSNkZv6TfnO+Lpc3Qg== X-Received: by 2002:a19:641b:0:b0:4f8:451d:d2cf with SMTP id y27-20020a19641b000000b004f8451dd2cfmr1301114lfb.69.1686840291885; Thu, 15 Jun 2023 07:44:51 -0700 (PDT) Received: from localhost.localdomain (mail.pulsar-telecom.ru. [94.181.180.60]) by smtp.gmail.com with ESMTPSA id h7-20020ac25967000000b004f13f4ec267sm244451lfp.186.2023.06.15.07.44.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Jun 2023 07:44:51 -0700 (PDT) From: Aleksandr Shubin To: linux-kernel@vger.kernel.org Cc: Aleksandr Shubin , Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Paul Walmsley , Palmer Dabbelt , Albert Ou , Philipp Zabel , Cristian Ciocaltea , Maxime Ripard , linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-riscv@lists.infradead.org Subject: [PATCH v1 1/3] dt-bindings: pwm: Add binding for Allwinner D1/T113-S3/R329 PWM controller Date: Thu, 15 Jun 2023 17:43:56 +0300 Message-Id: <20230615144423.828698-2-privatesub2@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230615144423.828698-1-privatesub2@gmail.com> References: <20230615144423.828698-1-privatesub2@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230615_074454_945255_B2AE1741 X-CRM114-Status: GOOD ( 13.89 ) 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 Allwinner's D1, T113-S3 and R329 SoCs have a new pwm controller witch is different from the previous pwm-sun4i. D1 and T113s SoCs have one PWM controller with 8 channels. R329 SoC has two PWM controllers in both power domains, one of them has 9 channels (CPUX one) and the other has 6 (CPUS one). Add a device tree binding for them. Signed-off-by: Aleksandr Shubin --- .../bindings/pwm/allwinner,sun20i-pwm.yaml | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Documentation/devicetree/bindings/pwm/allwinner,sun20i-pwm.yaml diff --git a/Documentation/devicetree/bindings/pwm/allwinner,sun20i-pwm.yaml b/Documentation/devicetree/bindings/pwm/allwinner,sun20i-pwm.yaml new file mode 100644 index 000000000000..e5f9cb2d5c4f --- /dev/null +++ b/Documentation/devicetree/bindings/pwm/allwinner,sun20i-pwm.yaml @@ -0,0 +1,70 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/pwm/allwinner,sun20i-pwm.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Allwinner D1, T113-S3 and R329 PWM + +maintainers: + - Chen-Yu Tsai + - Maxime Ripard + +allOf: + - $ref: pwm.yaml# + +properties: + compatible: + const: allwinner,sun20i-d1-pwm + + reg: + maxItems: 1 + + "#pwm-cells": + const: 3 + + clocks: + items: + - description: 24 MHz oscillator + - description: Bus Clock + + clock-names: + items: + - const: hosc + - const: bus + + resets: + items: + - description: module reset + + allwinner,pwm-channels: + $ref: /schemas/types.yaml#/definitions/uint32 + description: The number of PWM channels configured for this instance + +required: + - compatible + - reg + - "#pwm-cells" + - clocks + - clock-names + - resets + - allwinner,pwm-channels + +additionalProperties: false + +examples: + - | + #include + #include + + pwm: pwm@2000c00 { + compatible = "allwinner,sun20i-d1-pwm"; + reg = <0x02000c00 0x400>; + clocks = <&dcxo>, <&ccu CLK_BUS_PWM>; + clock-names = "hosc", "bus"; + resets = <&ccu RST_BUS_PWM>; + allwinner,pwm-channels = <8>; + #pwm-cells = <0x3>; + }; + +... From patchwork Thu Jun 15 14:43:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aleksandr Shubin X-Patchwork-Id: 13281369 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 B5580EB64DB for ; Thu, 15 Jun 2023 14:45:19 +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=ILRU8K+xCtLwOb21SdzD+egtXbwHohNkPv3/f4SiywI=; b=iW/8Hh+KsbftQm rIwbKaXXNfGxPMBrh5oH+zEAwRnSipM52JgKpDF3I+vL86H07ID8cs7PpG1ajOOQDDkdJ5JsSCRqp w3UkBHeAaZbCS8mDtYYY6ZyVZfhNfjTL+eLgaezqGfZNFk89VPwUdRUKahlmqwwYAGFzjkemvLcJo X4u9TnojfGSo6GDfcEv6W352pk974xCMSR+6MPhXce9+lCPQ6PKXyBHpjmUzk7QSi9YcdsuTqe3e4 KSEWIwXk9VfVj02dli/V65kb7nSVoe0fcgeikQJA/1f8bJz1GQhbkSfZmIDk1rTD+8TFFDat1D7RJ X4UatO6cVIpXKaMl/0nQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q9oDr-00FC3h-1r; Thu, 15 Jun 2023 14:45:15 +0000 Received: from mail-lf1-x133.google.com ([2a00:1450:4864:20::133]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q9oDn-00FC1F-0r; Thu, 15 Jun 2023 14:45:13 +0000 Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-4f611ac39c5so10878061e87.2; Thu, 15 Jun 2023 07:45:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686840309; x=1689432309; 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=Od+eFJu5Un7KQxaLhPRr3IFDtHMK7wuQZd5iaWBOH10=; b=fUz6JLSdDRz/tVup/zvfXMmNhkAvFf4elZjb866w5j9o8w7UZJvZ4fYnShm7SWfW9s pJFq27oqPsbABnjkpLREi8tW7t9kfgg7ogacwDKLOfAmx28X9DX53cCvZHFf3DSrTRsq DHY7Wfwn6QbUf9ArWK/S/TuneYwnSOo0/3Cb2ua2lZjdADxh0ZIiEqWju7JP0yO3XlMF YqIXoqMPPCGG8qKA/8Y79q/jYLQwwBKepzCeTMaDB0jnL5wjpqhH9vnA9PjK93Gh3r+6 oLGoXXVElJVzhnXNNPwAb0UldabuNvXSrVkjpkvEV41HdzLA4FfAdqIIwQkVdzBXXkEz ikZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686840309; x=1689432309; 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=Od+eFJu5Un7KQxaLhPRr3IFDtHMK7wuQZd5iaWBOH10=; b=X+e2LvB+6CIraKRwzsWZV+rMgH7DEpmNfMA4xx4clsfQHp5LCTOaWDrMDRwmnliQdx 3PBP4G4VmWHVWkePeM+VzflLVmDAzqHjvxv9e0JM7zihW/kBMv2mUSr9bfgG1KqG9SsM 5e2nXH8WuGNivpxfUuh6+BeUzhcx6fPd6At4mC5zWnpSE1EQ4vF0wvrkiOGOK2jbV9pU oxamMN3WWs/E/LJoBne+aZpN2JzCybyeSp+rRaq8WS4i8mmtq6wXOrwjvO4Q3ZcaZy78 NxZEomfFxsx+ZkQCbDZRmDCwFuSrkBazkebiifzMva2v76YNuzl6Ta5ZFkx5j7v1YUXK Ebfg== X-Gm-Message-State: AC+VfDyIo3c+gR33DMndBU1DllCYo3z8ytrHPR9122zaycOXMFJjthPd N+1c3/2pMunHKSljafR2EQ== X-Google-Smtp-Source: ACHHUZ42J/0Jf+gDaLLKGQHbIyIlptOWp+NsU/XsK+8bJkVec+s53U9ItXSWHV2vOSUE3URFiJCKgQ== X-Received: by 2002:a19:6d1c:0:b0:4ef:fb4b:eb2d with SMTP id i28-20020a196d1c000000b004effb4beb2dmr9341785lfc.33.1686840309008; Thu, 15 Jun 2023 07:45:09 -0700 (PDT) Received: from localhost.localdomain (mail.pulsar-telecom.ru. [94.181.180.60]) by smtp.gmail.com with ESMTPSA id h7-20020ac25967000000b004f13f4ec267sm244451lfp.186.2023.06.15.07.45.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Jun 2023 07:45:08 -0700 (PDT) From: Aleksandr Shubin To: linux-kernel@vger.kernel.org Cc: Aleksandr Shubin , Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Paul Walmsley , Palmer Dabbelt , Albert Ou , Philipp Zabel , Cristian Ciocaltea , Heiko Stuebner , Maxime Ripard , linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-riscv@lists.infradead.org Subject: [PATCH v1 2/3] pwm: Add Allwinner's D1/T113-S3/R329 SoCs PWM support Date: Thu, 15 Jun 2023 17:43:57 +0300 Message-Id: <20230615144423.828698-3-privatesub2@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230615144423.828698-1-privatesub2@gmail.com> References: <20230615144423.828698-1-privatesub2@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230615_074511_311308_F048F9F6 X-CRM114-Status: GOOD ( 31.05 ) 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 Allwinner's D1, T113-S3 and R329 SoCs have a quite different PWM controllers with ones supported by pwm-sun4i driver. This patch adds a PWM controller driver for Allwinner's D1, T113-S3 and R329 SoCs. The main difference between these SoCs is the number of channels defined by the DT property. Signed-off-by: Aleksandr Shubin --- drivers/pwm/Kconfig | 12 ++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-sun20i.c | 364 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 377 insertions(+) create mode 100644 drivers/pwm/pwm-sun20i.c diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig index 8df861b1f4a3..b435e50fbd3e 100644 --- a/drivers/pwm/Kconfig +++ b/drivers/pwm/Kconfig @@ -594,6 +594,18 @@ config PWM_SUN4I To compile this driver as a module, choose M here: the module will be called pwm-sun4i. +config PWM_SUN20I + tristate "Allwinner D1/T113s/R329 PWM support" + depends on ARCH_SUNXI || COMPILE_TEST + depends on COMMON_CLK + help + Generic PWM framework driver for Allwinner D1/T113s/R329 SoCs. + The main difference between these SoCs is the number of + channels defined by the DT property. + + To compile this driver as a module, choose M here: the module + will be called pwm-sun20i. + config PWM_SUNPLUS tristate "Sunplus PWM support" depends on ARCH_SUNPLUS || COMPILE_TEST diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile index 19899b912e00..cea872e22c78 100644 --- a/drivers/pwm/Makefile +++ b/drivers/pwm/Makefile @@ -55,6 +55,7 @@ obj-$(CONFIG_PWM_STM32) += pwm-stm32.o obj-$(CONFIG_PWM_STM32_LP) += pwm-stm32-lp.o obj-$(CONFIG_PWM_STMPE) += pwm-stmpe.o obj-$(CONFIG_PWM_SUN4I) += pwm-sun4i.o +obj-$(CONFIG_PWM_SUN20I) += pwm-sun20i.o obj-$(CONFIG_PWM_SUNPLUS) += pwm-sunplus.o obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o obj-$(CONFIG_PWM_TIECAP) += pwm-tiecap.o diff --git a/drivers/pwm/pwm-sun20i.c b/drivers/pwm/pwm-sun20i.c new file mode 100644 index 000000000000..100b0f3bcec0 --- /dev/null +++ b/drivers/pwm/pwm-sun20i.c @@ -0,0 +1,364 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * PWM Controller Driver for sunxi platforms (D1, T113-S3 and R329) + * + * Copyright (c) 2023 Aleksandr Shubin + */ + +#include +#include +#include +#include +#include +#include +#include + +#define PWM_CLK_CFG_REG(chan) (0x20 + (((chan) >> 1) * 0x4)) +#define PWM_CLK_SRC 7 +#define PWM_CLK_SRC_MASK GENMASK(8, PWM_CLK_SRC) +#define PWM_CLK_DIV_M 0 +#define PWM_CLK_DIV_M_MASK GENMASK(3, PWM_CLK_DIV_M) + +#define PWM_CLK_GATE_REG 0x40 +#define PWM_CLK_GATING(chan) BIT(chan) + +#define PWM_ENABLE_REG 0x80 +#define PWM_EN(chan) BIT(chan) + +#define PWM_CTL_REG(chan) (0x100 + (chan) * 0x20) +#define PWM_ACT_STA BIT(8) +#define PWM_PRESCAL_K 0 +#define PWM_PRESCAL_K_MASK GENMASK(7, PWM_PRESCAL_K) + +#define PWM_PERIOD_REG(chan) (0x104 + (chan) * 0x20) +#define PWM_ENTIRE_CYCLE 16 +#define PWM_ENTIRE_CYCLE_MASK GENMASK(31, PWM_ENTIRE_CYCLE) +#define PWM_ACT_CYCLE 0 +#define PWM_ACT_CYCLE_MASK GENMASK(15, PWM_ACT_CYCLE) + +#define SET_VALUE(reg_val, val, name) \ + (reg_val = (((reg_val) & ~name##_MASK) | ((val) << (name)))) +#define GET_VALUE(reg_val, name) \ + (((reg_val) & ~name##_MASK) >> (name)) + +struct sun20i_pwm_chip { + struct pwm_chip chip; + struct clk *clk_bus, *clk_hosc; + struct reset_control *rst; + void __iomem *base; + /* Mutex to protect pwm apply state */ + struct mutex mutex; +}; + +static inline struct sun20i_pwm_chip *to_sun20i_pwm_chip(struct pwm_chip *chip) +{ + return container_of(chip, struct sun20i_pwm_chip, chip); +} + +static inline u32 sun20i_pwm_readl(struct sun20i_pwm_chip *chip, + unsigned long offset) +{ + return readl(chip->base + offset); +} + +static inline void sun20i_pwm_writel(struct sun20i_pwm_chip *chip, + u32 val, unsigned long offset) +{ + writel(val, chip->base + offset); +} + +static int sun20i_pwm_get_state(struct pwm_chip *chip, + struct pwm_device *pwm, + struct pwm_state *state) +{ + struct sun20i_pwm_chip *sun20i_chip = to_sun20i_pwm_chip(chip); + u64 clk_rate, tmp; + u32 val; + u16 clk_div, act_cycle; + u8 prescal, div_id; + u8 chn = pwm->hwpwm; + + mutex_lock(&sun20i_chip->mutex); + + val = sun20i_pwm_readl(sun20i_chip, PWM_CLK_CFG_REG(pwm->hwpwm)); + div_id = GET_VALUE(val, PWM_CLK_DIV_M); + if (GET_VALUE(val, PWM_CLK_SRC) == 0) + clk_rate = clk_get_rate(sun20i_chip->clk_hosc); + else + clk_rate = clk_get_rate(sun20i_chip->clk_bus); + + val = sun20i_pwm_readl(sun20i_chip, PWM_CTL_REG(pwm->hwpwm)); + if (PWM_ACT_STA & val) + state->polarity = PWM_POLARITY_NORMAL; + else + state->polarity = PWM_POLARITY_INVERSED; + + prescal = PWM_PRESCAL_K & val; + + val = sun20i_pwm_readl(sun20i_chip, PWM_ENABLE_REG); + if (PWM_EN(chn) & val) + state->enabled = true; + else + state->enabled = false; + + val = sun20i_pwm_readl(sun20i_chip, PWM_PERIOD_REG(pwm->hwpwm)); + act_cycle = GET_VALUE(val, PWM_ACT_CYCLE); + clk_div = GET_VALUE(val, PWM_ENTIRE_CYCLE); + + tmp = act_cycle * prescal * (1U << div_id) * NSEC_PER_SEC; + state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate); + tmp = clk_div * prescal * (1U << div_id) * NSEC_PER_SEC; + state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate); + + mutex_unlock(&sun20i_chip->mutex); + + return 0; +} + +static int sun20i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, + const struct pwm_state *state) +{ + struct pwm_state curstate; + int ret = 0; + u32 clk_gate, clk_cfg, pwm_en, ctl, period; + u64 bus_rate, hosc_rate, clk_div, val, clk_rate; + u16 prescaler, div_m; + bool use_bus_clk; + struct sun20i_pwm_chip *sun20i_chip = to_sun20i_pwm_chip(chip); + + mutex_lock(&sun20i_chip->mutex); + pwm_get_state(pwm, &curstate); + + pwm_en = sun20i_pwm_readl(sun20i_chip, PWM_ENABLE_REG); + if (state->polarity != curstate.polarity || + state->duty_cycle != curstate.duty_cycle || + state->period != curstate.period) { + ctl = sun20i_pwm_readl(sun20i_chip, PWM_CTL_REG(pwm->hwpwm)); + clk_cfg = sun20i_pwm_readl(sun20i_chip, PWM_CLK_CFG_REG(pwm->hwpwm)); + hosc_rate = clk_get_rate(sun20i_chip->clk_hosc); + bus_rate = clk_get_rate(sun20i_chip->clk_bus); + + if (pwm_en & PWM_EN(pwm->hwpwm ^ 1)) { + /* check period only */ + if (GET_VALUE(clk_cfg, PWM_CLK_SRC) == 0) + clk_rate = hosc_rate; + else + clk_rate = bus_rate; + + val = state->period * clk_rate; + do_div(val, NSEC_PER_SEC); + + div_m = GET_VALUE(clk_cfg, PWM_CLK_DIV_M); + + /* calculate and set prescaler, PWM entire cycle */ + clk_div = val; + for (prescaler = 0; clk_div > 65535; prescaler++) { + if (prescaler >= 256) { + dev_err(sun20i_chip->chip.dev, "Period is too long\n"); + ret = -EINVAL; + goto unlock_mutex; + } + + clk_div = val; + do_div(clk_div, 1U << div_m); + do_div(clk_div, prescaler + 1); + } + } else { + /* check period and select clock source */ + use_bus_clk = false; + val = state->period * hosc_rate; + do_div(val, NSEC_PER_SEC); + if (val <= 1) { + use_bus_clk = true; + val = state->period * bus_rate; + do_div(val, NSEC_PER_SEC); + if (val <= 1) { + dev_err(sun20i_chip->chip.dev, "Period is too small\n"); + ret = -EINVAL; + goto unlock_mutex; + } + } + + if (use_bus_clk) + SET_VALUE(clk_cfg, 1, PWM_CLK_SRC); + else + SET_VALUE(clk_cfg, 0, PWM_CLK_SRC); + + /* calculate and set prescaler, M factor, PWM entire cycle */ + clk_div = val; + for (prescaler = div_m = 0; clk_div > 65535; prescaler++) { + if (prescaler >= 256) { + prescaler = 0; + div_m++; + if (div_m >= 9) { + dev_err(sun20i_chip->chip.dev, "Period is too long\n"); + ret = -EINVAL; + goto unlock_mutex; + } + } + + clk_div = val; + do_div(clk_div, 1U << div_m); + do_div(clk_div, prescaler + 1); + } + + /* set up the M factor */ + SET_VALUE(clk_cfg, div_m, PWM_CLK_DIV_M); + + sun20i_pwm_writel(sun20i_chip, clk_cfg, PWM_CLK_CFG_REG(pwm->hwpwm)); + } + + period = sun20i_pwm_readl(sun20i_chip, PWM_PERIOD_REG(pwm->hwpwm)); + + SET_VALUE(period, clk_div, PWM_ENTIRE_CYCLE); + SET_VALUE(ctl, prescaler, PWM_PRESCAL_K); + + /* set duty cycle */ + val = state->period; + do_div(val, clk_div); + clk_div = state->duty_cycle; + do_div(clk_div, val); + if (clk_div > 65535) + clk_div = 65535; + + SET_VALUE(period, clk_div, PWM_ACT_CYCLE); + sun20i_pwm_writel(sun20i_chip, period, PWM_PERIOD_REG(pwm->hwpwm)); + + if (state->polarity == PWM_POLARITY_NORMAL) + ctl |= PWM_ACT_STA; + else + ctl &= ~PWM_ACT_STA; + + sun20i_pwm_writel(sun20i_chip, ctl, PWM_CTL_REG(pwm->hwpwm)); + } + + if (state->enabled != curstate.enabled) { + clk_gate = sun20i_pwm_readl(sun20i_chip, PWM_CLK_GATE_REG); + + if (state->enabled) { + clk_gate |= PWM_CLK_GATING(pwm->hwpwm); + pwm_en |= PWM_EN(pwm->hwpwm); + } else { + clk_gate &= ~PWM_CLK_GATING(pwm->hwpwm); + pwm_en &= ~PWM_EN(pwm->hwpwm); + } + sun20i_pwm_writel(sun20i_chip, pwm_en, PWM_ENABLE_REG); + sun20i_pwm_writel(sun20i_chip, clk_gate, PWM_CLK_GATE_REG); + } + +unlock_mutex: + mutex_unlock(&sun20i_chip->mutex); + + return ret; +} + +static const struct pwm_ops sun20i_pwm_ops = { + .get_state = sun20i_pwm_get_state, + .apply = sun20i_pwm_apply, + .owner = THIS_MODULE, +}; + +static const struct of_device_id sun20i_pwm_dt_ids[] = { + { .compatible = "allwinner,sun20i-d1-pwm" }, + { }, +}; +MODULE_DEVICE_TABLE(of, sun20i_pwm_dt_ids); + +static int sun20i_pwm_probe(struct platform_device *pdev) +{ + struct sun20i_pwm_chip *sun20i_chip; + int ret; + + sun20i_chip = devm_kzalloc(&pdev->dev, sizeof(*sun20i_chip), GFP_KERNEL); + if (!sun20i_chip) + return -ENOMEM; + + sun20i_chip->base = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(sun20i_chip->base)) + return PTR_ERR(sun20i_chip->base); + + sun20i_chip->clk_bus = devm_clk_get(&pdev->dev, "bus"); + if (IS_ERR(sun20i_chip->clk_bus)) { + dev_err(&pdev->dev, "Failed to get bus clock\n"); + return PTR_ERR(sun20i_chip->clk_bus); + } + + sun20i_chip->clk_hosc = devm_clk_get(&pdev->dev, "hosc"); + if (IS_ERR(sun20i_chip->clk_hosc)) { + dev_err(&pdev->dev, "Failed to get hosc clock\n"); + return PTR_ERR(sun20i_chip->clk_hosc); + } + + sun20i_chip->rst = devm_reset_control_get(&pdev->dev, NULL); + if (IS_ERR(sun20i_chip->rst)) { + dev_err(&pdev->dev, "Failed to get bus reset\n"); + return PTR_ERR(sun20i_chip->rst); + } + + /* Deassert reset */ + ret = reset_control_deassert(sun20i_chip->rst); + if (ret) { + dev_err(&pdev->dev, "Failed to deassert reset\n"); + return ret; + } + + ret = clk_prepare_enable(sun20i_chip->clk_bus); + if (ret) { + dev_err(&pdev->dev, "Failed to ungate bus clock\n"); + goto err_bus; + } + + ret = of_property_read_u32(pdev->dev.of_node, + "allwinner,pwm-channels", + &sun20i_chip->chip.npwm); + if (ret) { + dev_err(&pdev->dev, "Can't get pwm-channels\n"); + goto err_pwm_add; + } + + sun20i_chip->chip.dev = &pdev->dev; + sun20i_chip->chip.ops = &sun20i_pwm_ops; + + mutex_init(&sun20i_chip->mutex); + + ret = pwmchip_add(&sun20i_chip->chip); + if (ret < 0) { + dev_err(&pdev->dev, "Failed to add PWM chip: %d\n", ret); + goto err_pwm_add; + } + + platform_set_drvdata(pdev, sun20i_chip); + + return 0; + +err_pwm_add: + clk_disable_unprepare(sun20i_chip->clk_bus); +err_bus: + reset_control_assert(sun20i_chip->rst); + return ret; +} + +static void sun20i_pwm_remove(struct platform_device *pdev) +{ + struct sun20i_pwm_chip *sun20i_chip = platform_get_drvdata(pdev); + int ret; + + pwmchip_remove(&sun20i_chip->chip); + + clk_disable_unprepare(sun20i_chip->clk_bus); + reset_control_assert(sun20i_chip->rst); +} + +static struct platform_driver sun20i_pwm_driver = { + .driver = { + .name = "sun20i-pwm", + .of_match_table = sun20i_pwm_dt_ids, + }, + .probe = sun20i_pwm_probe, + .remove_new = sun20i_pwm_remove, +}; +module_platform_driver(sun20i_pwm_driver); + +MODULE_AUTHOR("Aleksandr Shubin "); +MODULE_DESCRIPTION("Allwinner sun20i PWM driver"); +MODULE_LICENSE("GPL"); From patchwork Thu Jun 15 14:43:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aleksandr Shubin X-Patchwork-Id: 13281370 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 8F55DEB64DA for ; Thu, 15 Jun 2023 14:45:43 +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=OCUV5ZLmrRA4WewQh3y9OmSwaIXI7VIKXd555YDjvqI=; b=KpE71CNb1EvrHw zKtqs04SQ1rKlQ6kj+9i1p8QbMxLBIhaq8Qb6DGcSsgtEHXaM3OKvuCkGlXPOTbhyoejJoO13XwXO QKN8jFLpIv2y47CeuV/zUNwnAJOwX0MrLPyY/GrDFB58AaupuD31ndBdZ+Dfg1TxE8eoCtdbSETKS xIa8EACBFTe84gFCjca9VHxyEEOhSuTNTHgRL1Tc/Tq0R4NhhIaMl6IwJxZq8gvkX4mjOfQ2+0hBK zz4+hbmCaann4p6dQJqWFumWg5yXl5wg1Lx5GtELuooHXImvmD9LpM7QzjwszqJ4EXw4eKIKDsNjO c6YNZ5DqJ3DRjPlY8Ydw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q9oEF-00FCCN-0u; Thu, 15 Jun 2023 14:45:39 +0000 Received: from mail-lf1-x12a.google.com ([2a00:1450:4864:20::12a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q9oEC-00FCAL-0x; Thu, 15 Jun 2023 14:45:37 +0000 Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-4f76a223ca5so2606280e87.3; Thu, 15 Jun 2023 07:45:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686840334; x=1689432334; 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=wV92pSpCVmnx9n4E1q+MRb4GksMGgD+y07OYM0/qT58=; b=aIzfv4Mjupmq37gyHvFs8F/LSK3Xj9YVHKM2j607IKnuOkrvGZAVssXNMRXeWX+dmV /AABX0+ICsavEYJX+R3R3oC/NUjWUAt54gfecZ3dQ96G9nKrk4AmGMC0G7j9Lbv1SgaH HquSTWXbpgXY4d8vkgRan3VK+R5OFrH+2Tzl6vO74LPZSu7DHUE/j3uzddwaTKvx2H/t 5202qourcPHXWuCglvCP6CdoV+V9kEbAap/hK6RIej9iv8rZpwVYy0hFRMU8uItAvMrz 5CsyEKHBt1Sfnna5pi9Im9hGJRY1hJVT/ZiTBhH9OukB8sEog1MjIDrMhfm4o4YpicQL mQiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686840334; x=1689432334; 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=wV92pSpCVmnx9n4E1q+MRb4GksMGgD+y07OYM0/qT58=; b=iIWVheGAbi5pzUnPISxksFbu/cdJtBKLcCy6HFKMEdt0VjXkJeN6eAp4MD9S13Uy3w 7+1c4NMP9CLnTYdP7Db3FSLnhbp1CJVFAkZ8JR1MNQpBdyXrePQHnHDCRE33hcRS9MaY fty6vWJeWR4kJ+C9TwYMgCBranKqB/A/bB80JUMM0mO+IAEVd3oGpfE2d+Cz/BxHpvCC u/XHm8t96ObeVUARjlz7/LzVlt52x483TDmmPLLQ6EbnnAcmluf6dVUrDyVBbCF6Vi1Z b94FzBdsk36w6gyOgTulP0PjrZLrk22Qsfs439FJayXxTRiGuNPqC50inZOBOEpC2PCg sAjQ== X-Gm-Message-State: AC+VfDw7IBrwbVAsA2bTcvab1W5adCQVFbZ5XmkhqiUHDefT4ygNhl/b ein/qvm3N/9F/30+Sdcn5Q== X-Google-Smtp-Source: ACHHUZ7JCXFEpcyRUkA34U51/Dy11OR5ITEbLUmrZUsxSsG6Is9Uo9yDnK3rcDKYLGftkk2WwnqeoQ== X-Received: by 2002:a19:e01e:0:b0:4f8:4348:e000 with SMTP id x30-20020a19e01e000000b004f84348e000mr1324141lfg.13.1686840333381; Thu, 15 Jun 2023 07:45:33 -0700 (PDT) Received: from localhost.localdomain (mail.pulsar-telecom.ru. [94.181.180.60]) by smtp.gmail.com with ESMTPSA id h7-20020ac25967000000b004f13f4ec267sm244451lfp.186.2023.06.15.07.45.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Jun 2023 07:45:32 -0700 (PDT) From: Aleksandr Shubin To: linux-kernel@vger.kernel.org Cc: Aleksandr Shubin , Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Paul Walmsley , Palmer Dabbelt , Albert Ou , Philipp Zabel , Cristian Ciocaltea , Andre Przywara , Maxime Ripard , linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-riscv@lists.infradead.org Subject: [PATCH v1 3/3] riscv: dts: allwinner: d1: Add pwm node Date: Thu, 15 Jun 2023 17:43:58 +0300 Message-Id: <20230615144423.828698-4-privatesub2@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230615144423.828698-1-privatesub2@gmail.com> References: <20230615144423.828698-1-privatesub2@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230615_074536_339206_AE9AE401 X-CRM114-Status: GOOD ( 11.43 ) 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 D1 and T113s contain a pwm controller with 8 channels. This controller is supported by the sun20i-pwm driver. Add a device tree node for it. Signed-off-by: Aleksandr Shubin --- arch/riscv/boot/dts/allwinner/sunxi-d1s-t113.dtsi | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/riscv/boot/dts/allwinner/sunxi-d1s-t113.dtsi b/arch/riscv/boot/dts/allwinner/sunxi-d1s-t113.dtsi index 922e8e0e2c09..50f0f761527b 100644 --- a/arch/riscv/boot/dts/allwinner/sunxi-d1s-t113.dtsi +++ b/arch/riscv/boot/dts/allwinner/sunxi-d1s-t113.dtsi @@ -127,6 +127,18 @@ uart3_pb_pins: uart3-pb-pins { }; }; + pwm: pwm@2000c00 { + compatible = "allwinner,sun20i-d1-pwm"; + reg = <0x02000c00 0x400>; + clocks = <&dcxo>, + <&ccu CLK_BUS_PWM>; + clock-names = "hosc", "bus"; + resets = <&ccu RST_BUS_PWM>; + allwinner,pwm-channels = <8>; + status = "disabled"; + #pwm-cells = <0x3>; + }; + ccu: clock-controller@2001000 { compatible = "allwinner,sun20i-d1-ccu"; reg = <0x2001000 0x1000>;