From patchwork Tue Oct 8 03:03:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Wang X-Patchwork-Id: 13825590 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 94908CED25D for ; Tue, 8 Oct 2024 03:04:12 +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=XmIjfXBNJP120+Wq/HT1iPsGtiYQNHTWtSc7gIEv1jg=; b=Xzx5tq9ns6zy1j S5xaS7mJZ69ABfF8xbOwXVPwnvqqqzJOH+lslBgFSlGoKrfvt5jmncSpPa9Ta89m3qOwimH3DeZ0f 9irVGa6sAYfOJuOXw7iMlDoLeO73+k/XjOtlFzdN6RMue4x6CbE9ftkyiLZYSmMa5FCXEdJ/p+zqA 1xbwt8PPqh46YXR1ktogacJXnHilpaFKudqsHGFgla7HXGF5szrRYOcOTOhVtLaCo0xkI7rGyCUiy dDza1Qgmn+VWBhTT/04wvhMwGqNRbn7drgqgDpHwViWiQ3SyERwUGQx7bA9LNP5ACGyyh5RXYAktY NRCK3RUVhU7n73YPS1Cg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1sy0W8-00000004M31-0P6M; Tue, 08 Oct 2024 03:04:08 +0000 Received: from mail-ot1-x332.google.com ([2607:f8b0:4864:20::332]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1sy0W5-00000004M2U-2IUY for linux-riscv@lists.infradead.org; Tue, 08 Oct 2024 03:04:06 +0000 Received: by mail-ot1-x332.google.com with SMTP id 46e09a7af769-715716974baso276125a34.1 for ; Mon, 07 Oct 2024 20:04:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728356644; x=1728961444; 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=UlWzcYJ31J9Ws/z9sFG6bpzdTEyI/n4+1H05qgKO0vE=; b=BQlAJou6Z5WjKL8UUCvNFT63B6brO8XQrWTSkNXWcMLybMirxYojucXxkMyvW5hVic DIaPyixAL+QU/9Qfr+N/KQ5ovDKtQKfb23dB/+nKFfXCmWYgiGf01NrWObuyAY0RF0oC tPt9+H2DTW4aORs0iNS+cy9iRr1yoNZqxqrdlAx7idDLqGXaj6WejN3SgQMPBYSAl72g plA0YFXiPC62msaDWjBIWvf26YYRXyURYJuH1oh70rEv2NaIj60VGCDUGUG9qkG46p2F iJ7u6q+hUhSKyHHSEOU8FwwKJ9w4/t0z1BWED7GZ7W2XHSi/1iwJ4vwdqHEPgp4ZN2Dh Ufxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728356644; x=1728961444; 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=UlWzcYJ31J9Ws/z9sFG6bpzdTEyI/n4+1H05qgKO0vE=; b=Qa1iWwIA5OveQ5KojAW9w6rvXTynTQygwXESBd1Vfek0kZjtZjz9DqaZ7WfYOYATPs 3GmNGAZwk6K4CuU/a7Wc1H2PXpqxh9J+/9t3c9fGiWghHGmtxDskSk1rWLKLvUQv8BXk dmyPHMHeRJH46/nAu5l70QHrNAyZT0ipMeZeBO7tTfkCT+FkyJdgjNVzcGVo5hEAvqll 1u91V4SfIcwEsWZbBf70OOnu3jt6ySQgLvFvpiGKQ/aZlvRYQfEi7+jf+KbXuHyerRy+ VfUB32YceqP/UZx+sxb5hBAns/4AZJQLfPA/5Pjn0TRjOCyk3c243zC2Hvf1cKYC7+pw I3RA== X-Forwarded-Encrypted: i=1; AJvYcCVg7do6a3xJV0aFPJyEJvOxXtxdO2JUH8bIT7nc32kixTlZGN5bLZmT4mD2tZxXjBa7TchNarnVaL+s6g==@lists.infradead.org X-Gm-Message-State: AOJu0Yw7wS37k8drrv/bqflGKiz1bC9RaKebwGV2DsTedHVT+ln+ZYiE nG3llVdMDBr6s3hW921UbJKRrvQYFXUJ/rCjLSNTLrwnb/Z04SrBoc6Dj+tj X-Google-Smtp-Source: AGHT+IG1ZpRLRDMCNzaG4ln3251H+l6NrmeOXdEcvw8EjEFUlcXPz+rrmFhK4pf1EQHHupmW9XupPw== X-Received: by 2002:a05:6830:2682:b0:710:edaa:c030 with SMTP id 46e09a7af769-7154e97bbe8mr8716722a34.26.1728356644416; Mon, 07 Oct 2024 20:04:04 -0700 (PDT) Received: from localhost.localdomain ([122.8.183.87]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-71556865db1sm1768505a34.72.2024.10.07.20.04.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 20:04:03 -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 v3 1/3] dt-bindings: pwm: sophgo: add PWM controller for SG2042 Date: Tue, 8 Oct 2024 11:03:54 +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-20241007_200405_615453_1CA65A64 X-CRM114-Status: GOOD ( 11.75 ) 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 | 51 +++++++++++++++++++ 1 file changed, 51 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..fe89719ed9dd --- /dev/null +++ b/Documentation/devicetree/bindings/pwm/sophgo,sg2042-pwm.yaml @@ -0,0 +1,51 @@ +# 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 + + "#pwm-cells": + const: 2 + +required: + - compatible + - reg + - clocks + - clock-names + +unevaluatedProperties: false + +examples: + - | + pwm@7f006000 { + compatible = "sophgo,sg2042-pwm"; + reg = <0x7f006000 0x1000>; + #pwm-cells = <2>; + clocks = <&clock 67>; + clock-names = "apb"; + }; From patchwork Tue Oct 8 03:04:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Wang X-Patchwork-Id: 13825591 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 EF238CED25D for ; Tue, 8 Oct 2024 03:04:30 +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=d9+2yDAz4pO0AcNmMtl36ooOcj30D+HUcL9IdiyZIis=; b=BY1iHJ4ikJbFnb S0z1jahuKBfZMM/rnPeKRPLRp8fgOCy/EqaMcF13leZLefMEKlf0xPuQGi7k1eoQ/r+Ee1EinD5tp ygBwrULorHDkI37AiYHzFB8ogvueW6BckfEF4pmZcPyG10U5KxAydmKF8jnrVAknBFZbH9PwUn5Lv H/uXjPToRVm1VwWQX9XRACTrAo6Q7kujpth/nFNZ8+a3WqM5lmiT7tOXCvvCAJFQvdt8UmFhNt2Lo fgT3u0FTG+YZTMpD1P1lg3Z8drL1vVlhpmsR4tt++BYDgIaOtY7itXqjsMAqPQBNBj+FWe4+O9WaX ALuwpJivpIljYBg6fyjg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1sy0WR-00000004MA3-0b8N; Tue, 08 Oct 2024 03:04:27 +0000 Received: from mail-ot1-x331.google.com ([2607:f8b0:4864:20::331]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1sy0WO-00000004M8a-2DJ8 for linux-riscv@lists.infradead.org; Tue, 08 Oct 2024 03:04:25 +0000 Received: by mail-ot1-x331.google.com with SMTP id 46e09a7af769-7151008f002so2618729a34.2 for ; Mon, 07 Oct 2024 20:04:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728356663; x=1728961463; 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=HrwsSs/z/pCRHRshak8tpEDcJHcOvUffC1RAHP+5S5k=; b=fJuI7bf9GdDBbDup21MPIcP5Qp3Or2869LA2Oy7CPvvswaAdr8GrZvUz4gzOKrTLvb L4dR1XFyqnSi8rkcJCcEF4SfeTL83BiBKS+x0gXu87lsvMONALLT6bA/xWIgZkRFpUDP bc19pJATfP6qrAgiSml4IhZ42riJ3ao07Av9ry2it8+Cl2wu0uC5PDIjjmYWxWh4FcCn 1yk4CCY8uUhYtP/XXREp5BIrUVP+SlYZZp2RlZwNtPfxzl0fgIToHd94/ropvjmNuUjS RxPioQ48JMq/8ve3GQYVtz9Q5XYZW6gpeBPpCLgXMc/hVHXDRaQi5ylpWh5yiWlD7Pdo cPDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728356663; x=1728961463; 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=HrwsSs/z/pCRHRshak8tpEDcJHcOvUffC1RAHP+5S5k=; b=EdBo5vRJNt3TbJeDsRpQZ0M+tXxfeFRhjPEjJL2H9HLz4H0tu4cFfaByjEQLeb2I51 nyfUXwqauhiamTvIPdjECnbHi18l/mMIgy2ZjSe9qa67detWd+mjXs848unaPU9lj0ab VorScRD3mMBf6lYM3F5mDeuQiAWYEWZQelkaogx84pA3lPL4Q2yGm14OksxMmazcvZ4J fYnw7L7PtmPxjjHEv8QRg9IQ1YsG+j16Gq2rfpacLnSq0XUwFqPZns1rMNTvV4e5D8jH cuWPfKvGUIs2tVBCS8PjhVzDtTnwWxHTrnQwOtZ1xWuZbcrzFdrRzC8cloPBvOrAwUYL vOAA== X-Forwarded-Encrypted: i=1; AJvYcCXE2oJqgtGoBPvT7nDSsLyn8bnkELyTNfBqJS2HcLyz7EKLLSIjNNF0h/FmapKKSVbHbJJVlsa5ejn03g==@lists.infradead.org X-Gm-Message-State: AOJu0YwsENE1em5Gw0TSytwYgXSrpCU+wUELhA26SnTOvrWeI/SXnIpr OGPgeISr4vdB2kOIyE4TwroWrkQT3BOvrEmIDAFv+ziaq1w/ZeB/ X-Google-Smtp-Source: AGHT+IEb54nCKSmtX6R321kIvYFqIxBWK2RiPXmKmeYVbfI35Fr35utxmLX4AJ1AAe8oCgCz3NBYHw== X-Received: by 2002:a05:6830:6481:b0:711:3ed:4226 with SMTP id 46e09a7af769-7154e807f0fmr10858455a34.7.1728356663389; Mon, 07 Oct 2024 20:04:23 -0700 (PDT) Received: from localhost.localdomain ([122.8.183.87]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-71556847306sm1763569a34.51.2024.10.07.20.04.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 20:04:22 -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 v3 2/3] pwm: sophgo: add driver for Sophgo SG2042 PWM Date: Tue, 8 Oct 2024 11:04:14 +0800 Message-Id: <57cf7ac3b4c092df1a6962d310b6d2603ca26995.1728355974.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-20241007_200424_598042_A8E0CAF5 X-CRM114-Status: GOOD ( 26.53 ) 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 --- drivers/pwm/Kconfig | 10 ++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-sophgo-sg2042.c | 180 ++++++++++++++++++++++++++++++++ 3 files changed, 191 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..198019b751ad --- /dev/null +++ b/drivers/pwm/pwm-sophgo-sg2042.c @@ -0,0 +1,180 @@ +// 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 + +/* + * 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 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); + + chip->ops = &pwm_sg2042_ops; + + ret = devm_pwmchip_add(dev, chip); + if (ret < 0) + 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 Tue Oct 8 03:04:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Wang X-Patchwork-Id: 13825592 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 5359BCED25D for ; Tue, 8 Oct 2024 03:04:49 +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=CUhzx34cD/ciqgYtDH+wuwEvfBEANjjVWjCER2ggrTw=; b=c1s2izTLf2hOZn wLe9sqKISakfusFOgLwAXBMmEEdSwQKp2trgpn2fJaWYOIEKq5lGB+GdcAQbvtGPOtt/UvYf+iMvA eC16cySjK+AYlF14Gq8UkMKa6QVYgLZ1h1Rg8p7wBriR3aznKDThD3gKb4/boag/H7yE/oky5gaDS CeN4xg3xOmxC6d44dncme+teJB8KQTlG1Bcl10ALyHnmqt9tToCbXNIwWoy6hEZ0S5ASdE6GPGBWU kb2fSgWVmIX7nLnyufYm9tho6eww0pohYPyqThjNqU2s8ZaOzhRVU3O9VqiJy3NJRwYyY+9iCGza7 KKC1fwP4c2dIbUwYw22w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1sy0Wj-00000004MIj-1aWT; Tue, 08 Oct 2024 03:04:45 +0000 Received: from mail-oi1-x22a.google.com ([2607:f8b0:4864:20::22a]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1sy0Wg-00000004MGo-3eqG for linux-riscv@lists.infradead.org; Tue, 08 Oct 2024 03:04:44 +0000 Received: by mail-oi1-x22a.google.com with SMTP id 5614622812f47-3e06853e579so2931372b6e.0 for ; Mon, 07 Oct 2024 20:04:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728356682; x=1728961482; 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=qVj0JZ+cVfOjaLAf/i9q4f7a0NWdixO3jwQiTDHespQ=; b=mp5PEoSMtaLWDGjHXgbMP3oibSmW7zJPcsoA2Ur5Oui8oWivuWbN1EThrfEqDP8tgV pcsD0tL3ZCOlIe23RZdn67vqwJEk1wJ8z0+XhyYeitLWBHhCfqmp13O4Nvum8skuuAe1 YjS/Lzi+jb/dX6tdF+HfpVZ04ESW+IWo8YarSW/q4zxhwZlrf33IFWCAm66T0Wrm9B59 zd4h9cKAE2Udn7NVcLDu+9EI3PcT1/c7MyXMRrB6zVmNus/yVegxYNPC1KpeOzuUpap/ ufB1QxKToc7QmkgD3AWJv6JA35tHH1FDsJxDBO+l+BK19aNbA1/lcdy3gTxbO0RmM5c4 aaxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728356682; x=1728961482; 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=qVj0JZ+cVfOjaLAf/i9q4f7a0NWdixO3jwQiTDHespQ=; b=txbVqux0lm7bhiSdQhR4SNbAt4DbyoHScnNA8DWMLpHFqzo04fn0db8SxiIEqZIjuA FkDfQTv3K5Fb0tcBkpO9IhxCu6Hv2F9Pu+aq9DtHzifw6IX8xyPEYjVPdq1f/MZ1s7aT xHTHDwfx9Zy7KBlf7Julr37HmpuqKG50hYakHLguiCDCxhxyrFVabumH3mT1TVlkNvPo JN4Rp68YNWFTh2mq5RXnFuqTBm0ys5w0fYnRH8Nj/6n3k1dqXI1dn/kMb8u3lYr4SqQd oTZUuuWy2D105vx/qBnhRgKAA6c5291Ru7eCSKvWnmzeatkjPsb+uiS+B+qisopsnvu8 xgSg== X-Forwarded-Encrypted: i=1; AJvYcCUxPHaGYvLt628i+HOJ0cLyz4SbxIKHO92+4SQveWWNZQKdGmVSFHJbAB8TOfJ/Jw64qiP8WNSmYMGUag==@lists.infradead.org X-Gm-Message-State: AOJu0Yy5XxsxJGWucK3H+ARlZCCxBLOJ6tcSVd3bvZycnEwom7IGkJQN YLeQurTsuP/UZnXRV3AIGUL/MfVohi1er3vKuemSKMs44tFYVdvi8alQ4rKB X-Google-Smtp-Source: AGHT+IGf1MJT/PyvOPxEnoprcZ6XuCuypJ2wUdXV9XylGNoyRVkd8fbI1Fm5UNJ1EFp0uIMZBKBF0Q== X-Received: by 2002:a05:6808:23d1:b0:3e3:a90b:25d5 with SMTP id 5614622812f47-3e3c133e35bmr8119051b6e.18.1728356681788; Mon, 07 Oct 2024 20:04:41 -0700 (PDT) Received: from localhost.localdomain ([122.8.183.87]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3e3cf854961sm1282089b6e.10.2024.10.07.20.04.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 20:04:40 -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 v3 3/3] riscv: sophgo: dts: add pwm controller for SG2042 SoC Date: Tue, 8 Oct 2024 11:04:33 +0800 Message-Id: <960826e94a94c1c0a25261e4081de7fa82dff5f5.1728355974.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-20241007_200442_943156_DD263D51 X-CRM114-Status: UNSURE ( 9.09 ) 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 | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/riscv/boot/dts/sophgo/sg2042.dtsi b/arch/riscv/boot/dts/sophgo/sg2042.dtsi index 4e5fa6591623..048792b30617 100644 --- a/arch/riscv/boot/dts/sophgo/sg2042.dtsi +++ b/arch/riscv/boot/dts/sophgo/sg2042.dtsi @@ -165,6 +165,14 @@ 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"; + }; + pllclk: clock-controller@70300100c0 { compatible = "sophgo,sg2042-pll"; reg = <0x70 0x300100c0 0x0 0x40>;