From patchwork Tue Sep 10 02:08:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Wang X-Patchwork-Id: 1982892 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=gcZ8s1Oi; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45e3:2400::1; helo=sv.mirrors.kernel.org; envelope-from=linux-pwm+bounces-3179-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org [IPv6:2604:1380:45e3:2400::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X2nDQ491lz1y1s for ; Tue, 10 Sep 2024 12:08:22 +1000 (AEST) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 0B4E228512D for ; Tue, 10 Sep 2024 02:08:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A4A6513C9B3; Tue, 10 Sep 2024 02:08:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gcZ8s1Oi" X-Original-To: linux-pwm@vger.kernel.org Received: from mail-oo1-f45.google.com (mail-oo1-f45.google.com [209.85.161.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 28C5A81E; Tue, 10 Sep 2024 02:08:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725934099; cv=none; b=PCunEKMBUpvEEC4MeJ1+LzKWU0Wh9hN8K/rKO6i30PxVPfzXqnSvxC5dL3k9RgoigaPijQ208fNuarba21VU6RKylKCHEXbgCEEXQ5S3m7f7g2pJxH1gHKyWfEqkYxP7wtCj45IhrvZLgGpZA58+rAYckKIWuVCFX33rug3oZGs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725934099; c=relaxed/simple; bh=2babzPnAQlxLfuTrsLFEKBg3rrRN+wOi9aMa9e08msg=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BVLVQul/kvsHDmmaXIH4uSYL+en2k6ivryn2b4fP54xtsyP7cZuWyXrmnjo/FIQXcthRw191PIbt9IBA4MgGHy1g35FiLq2gEKqjM6DsTg6jgTqHEeHpoc2xnDhQh+ATvQoaMbrYPWQlM3Oc7nxTVJESUsrDBN+q1qB3iPAmtD4= 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=gcZ8s1Oi; arc=none smtp.client-ip=209.85.161.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 Received: by mail-oo1-f45.google.com with SMTP id 006d021491bc7-5e1ba05bf73so1480143eaf.3; Mon, 09 Sep 2024 19:08:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725934097; x=1726538897; 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=BI30WVVFXk39PBBMlPlqzRH+DTqK3h2z+UCMnQv1Ops=; b=gcZ8s1Oi24uHnKkj1KNQ5JCyaBIQddPAD0SiWTJ51bGmQmUJ7diRvUfX7DEl/FdO2i mJMh1xzb+hdcPZ2u3YsV60aZpgmI/GSws51vdYHINyKDasszD+izKpJfu2E8MWoSU6LE dCbVUiJoTXeieAknjPK87+yKaTHzKHSo50Jz9NNiY7pxVpld5d9AzSGCr8/0Py1snAOp cb1oxxvVbH6nmvm4RXg+KqMktlUsXB5qWdNB0gLUhuKmD34ZluTR6Kyx79bySXO7Tcqr cVFyv9DBWp3iww8qc7jmp8k4USmHWfkzhzpRN8oAximihVRALmnRsXoftCIu2Ac6OOoE gOwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725934097; x=1726538897; 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=BI30WVVFXk39PBBMlPlqzRH+DTqK3h2z+UCMnQv1Ops=; b=OQ/7RrsSk9Jfv7TE6QWesjvyDp+A/oRg/pKvdF8nrz6FyuQ7cSLnKOaRaMtkEkQJBf VRYyHPIEUR0nK8BXQ7WdVP7edLF6swC0hzSqURsnf9XqEXIDVJ5cAYDgyEjCM1GEXMRx 5aCSJ8JMpb2IC3qIIEFh5X11mlNZ/qN4sgxQvctIViKd/eY4ld9y7nkGQf1EeRWeGVRF uWWWKRkP+GFQAeuQlOnRRSSdGiMBY34cO8EKdAnicLPDAipMKMpgXtzqinvtmMUzHJKJ vUnVpZG7TxeRpabwU++naOUsMOwSlqSkPrle+FYyw6DpYZqlDKpQNEwxsYGLccltNP1L zETQ== X-Forwarded-Encrypted: i=1; AJvYcCUBR/qspY+QNU1N0VQN29p8et1mMN7oX27bE0NEN1r5bmmkXC6c1jCrhQIt+KMI/yJ8ruPmViPGChwAi4iK@vger.kernel.org, AJvYcCUIasJLAvzcGr3isoHcqose1IiFBMufU1mRQnWLBG/e1Uwy+6CdG4A4F9FzkLxKmipuok696hVDHX53@vger.kernel.org, AJvYcCUi3bFJUskCW4A00MZDjwmimhTKq2PFIi0hnRWuWedIhi/Z4f4asFyCaJ1Pu8hg4Es0R6HISPx8m/VS@vger.kernel.org X-Gm-Message-State: AOJu0Yw7hp8HRUI8ae5v+o3p+oKpHBTrTSlkUQSfCp1dTq3GkWDAS99p rd+flh/7t3toc/dy+4N8WIPtKyTx3hqW97hdu1EamoiLO6fbXYfRpvpBVFDuW6Q= X-Google-Smtp-Source: AGHT+IGFLWDdr/KERlkpx8BuwbDh6erVc7wsCpO7c1sCwII502DRfQ2scgY32Y/Ts3V/PR3030bhNw== X-Received: by 2002:a05:6830:3c08:b0:710:f76b:350b with SMTP id 46e09a7af769-710f76b35b2mr307494a34.5.1725934097103; Mon, 09 Sep 2024 19:08:17 -0700 (PDT) Received: from localhost.localdomain ([122.8.183.87]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-710f40989a7sm380982a34.59.2024.09.09.19.08.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2024 19:08:16 -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 v2 1/2] dt-bindings: pwm: sophgo: add PWM controller for SG2042 Date: Tue, 10 Sep 2024 10:08:10 +0800 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-pwm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 Sep 10 02:08:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Wang X-Patchwork-Id: 1982894 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=JqZLXeuB; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2604:1380:45e3:2400::1; helo=sv.mirrors.kernel.org; envelope-from=linux-pwm+bounces-3180-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org [IPv6:2604:1380:45e3:2400::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X2nDh6jKYz1y1s for ; Tue, 10 Sep 2024 12:08:36 +1000 (AEST) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id AF71628532A for ; Tue, 10 Sep 2024 02:08:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1D6811428F4; Tue, 10 Sep 2024 02:08:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JqZLXeuB" X-Original-To: linux-pwm@vger.kernel.org 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 5ACD273467; Tue, 10 Sep 2024 02:08:33 +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=1725934115; cv=none; b=p97mwVBrN68Yx0jdFCRK78WBjlmk47QVl3V1gcVRNP87FYFSEPnpRtJIciSQ3wqK7mIR0AuMoRJCkVDkTEYjkYC72J2tc2rOmpS2o1d0YIlJfgmVzJI3yuMwaeEEOugWsIFEeldNxtj5UPBpVjMSy65+5gsBXNQW8l+UXTh9L28= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725934115; c=relaxed/simple; bh=xuI2m8ZDcTZYOE6qtsEutp9iwcGt9nE8d2UQVgQGwXc=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IrH6VPW6RsMr65SLuOoTU0IKNGAw4NXSrxFc71xBFLUNa9m3KVszBxEiGJ02BcorMB2ZJR2kYGwC+3UL38p2Bb/UuhaAMkZp+DY0qQ6LP6xx2XC1Qd75vSqbpfBGkAMObT0QEjHFU9cvv1PXwqnDx+cuKpp4dTt4mBRtLBNRcEk= 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=JqZLXeuB; 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 Received: by mail-ot1-f50.google.com with SMTP id 46e09a7af769-710d77380cdso1701579a34.0; Mon, 09 Sep 2024 19:08:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725934112; x=1726538912; 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=61LOB963c0reRM1b7i4Xf550C2dkifjVL8GXx7YaxlE=; b=JqZLXeuB+m0d2aEjhvFw0dC0GvP34TqP/Yzv4lwLDaMsa2GwSsfBZbIdYmcLJTqHG5 S6X9++S2/+oyFmwjuYweVzmP8DZm2kY5lJey1Ngt6tuzCRemCdy8Uac7R5z79NXfC9fu NC4ySZlhzU2ehhiPqB2tTdakuRd6UaQdawA6tMnhKg7B4GM1dU5LpKPDBV2pSMP7mkVc TwUSGS14nmHj+g1e5LilSBJLN+5RGr+JD8yeHVSrT1fFOwEKGu/gGvahdbBNkQ56r8wQ aJNbKUtcpfTfaiHC7IUu9m3gplR6qJt99lmlLvRhd0a6Hy4pucBTD24IvU0k1sKX0bXv Ufjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725934112; x=1726538912; 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=61LOB963c0reRM1b7i4Xf550C2dkifjVL8GXx7YaxlE=; b=afQWDw/d9Va71DE/POvNm5fFnkmHLfi5fKVYjEnqyPschk2/MtWjdL51VnyUIm/EPm iTkbSMw9WbE10xbYrgKHBhKnMaaA/7+p57o2a34Yud1tCIlQogQqEcMGGUIj44uZ7PD9 3+BBjoJNu9PTu9yWICT+jo7jvPlLcfW+9gt7vE3ucx4Lv4Viht2SaIR7CItBO1G8P755 96tXZLNC1HSZeyVh2HXgGEMvlDzX83szuXEshHomt/6PBRpIU5c3q9X566+nO8qEQsCh ZXDxBAo77/IjgTUZQEjmBSRplk15Tm2PRa6GFQoVRrvP7Pi+kQnP2nb2zcFy+IlpP0XA Fq7g== X-Forwarded-Encrypted: i=1; AJvYcCUA6mv0XVs9MXrcNi0E3Xd6doDM5PWokB6sKubirj3CmJs3gYsC3PQgw9raRIcp6Wt7pDSovg5BN9HS@vger.kernel.org, AJvYcCVskcMaBvFFa2jZ6eXUxYM9u+pCbBQpVTmdQ6BaQskUvrSbOVokg9TlLfXFDBvmd20Nz0cE+AifDRS+nvr5@vger.kernel.org, AJvYcCXwPeGXW1Xr4z1ONLh6Q/m7EUCpfDIXkO6nBWPoCkWQI7Gp4vV5Tn82G/t2hPGLaPRU7vHKdsoyPPSL@vger.kernel.org X-Gm-Message-State: AOJu0YyVcHdpItvWkycKJ9lKMdh5JQPHOgbwyzcsVUzEBvJW9tzSravj TwG4v4q76uryFFiV4LhBnRVMs6qwm1dW+cyN1WydhORnniQaCrgZAwLGziF8QFk= X-Google-Smtp-Source: AGHT+IE2vPR/qjcTLgGYGRs2+UIdrx4oMV7VBm305dj7Y/944NVKU1/5y8dc06G7sdVzql2mhGsNwA== X-Received: by 2002:a05:6830:4993:b0:710:f536:a840 with SMTP id 46e09a7af769-710f536a9damr1161072a34.31.1725934112186; Mon, 09 Sep 2024 19:08:32 -0700 (PDT) Received: from localhost.localdomain ([122.8.183.87]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-710d9db3280sm1693616a34.61.2024.09.09.19.08.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2024 19:08:31 -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 v2 2/2] pwm: sophgo: add driver for Sophgo SG2042 PWM Date: Tue, 10 Sep 2024 10:08:25 +0800 Message-Id: <0f7568620c2ad3d21885e83182c51f4fe9202367.1725931796.git.unicorn_wang@outlook.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-pwm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 3e53838990f5..de8a36ecabbb 100644 --- a/drivers/pwm/Kconfig +++ b/drivers/pwm/Kconfig @@ -577,6 +577,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 0be4f3e6dd43..ef2555e83183 100644 --- a/drivers/pwm/Makefile +++ b/drivers/pwm/Makefile @@ -52,6 +52,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");