From patchwork Wed Nov 29 03:03:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yixun Lan X-Patchwork-Id: 10081393 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 86EAE602DC for ; Wed, 29 Nov 2017 03:04:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 782FD296A2 for ; Wed, 29 Nov 2017 03:04:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6D4A7296A9; Wed, 29 Nov 2017 03:04:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 05A98296A2 for ; Wed, 29 Nov 2017 03:04:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:To :From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=DVTwpj2U0KqZwdP9iELYMkikoyjCT3A3pkoiFfPeoQ8=; b=c/d0w4vIBTzIDd YLNOdzkHHKbmgz6sr/L7Io6Xz/KuRMVp5UC0I66mBBFaLbB+7rXNhaT2qRgpATw0UQFmJaUP+piD+ 98AEtE9vQFha17gfAI0gqvnfHuSNO0FKlNSrE2WNGiV26FpZSYlWjMu4hC4frnfTP1M99PzBQwqKB cVwzJ/rJnWyalCv517fffNz3IUCc/HiuuxK5jkp2ZPRzRERTKgXm+aXGuNuZ/jLF8ZRmL73INQrq5 MuNFdMLNGQW0HjkbkCRtYn0OMHVOycI5wvnE3KX+XeD0pXl6Lln2iTq1eNZdQqE2k1kIy1eU7Dlsk WtPS56+NpOADLZxf8hLA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1eJsfQ-0000CL-Ib; Wed, 29 Nov 2017 03:04:08 +0000 Received: from mail-sh2.amlogic.com ([58.32.228.45]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1eJsfN-00009T-Kd; Wed, 29 Nov 2017 03:04:07 +0000 Received: from localhost.localdomain (10.18.20.164) by mail-sh2.amlogic.com (10.18.11.6) with Microsoft SMTP Server id 15.0.1320.4; Wed, 29 Nov 2017 11:02:43 +0800 From: Yixun Lan To: Thierry Reding , , Subject: [PATCH] pwm: meson: fix harware duty calculation Date: Wed, 29 Nov 2017 11:03:08 +0800 Message-ID: <20171129030308.22036-1-yixun.lan@amlogic.com> X-Mailer: git-send-email 2.15.0 MIME-Version: 1.0 X-Originating-IP: [10.18.20.164] X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171128_190405_840426_955A72D2 X-CRM114-Status: GOOD ( 12.02 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Neil Armstrong , Kevin Hilman , Yixun Lan , linux-kernel@vger.kernel.org, Jian Hu , Carlo Caione , linux-arm-kernel@lists.infradead.org, Jerome Brunet Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Jian Hu The actual HIGH/LOW signal output from the PWM is equal to the value programed to HW register plus one, this is designed by HW. This fix should apply to all Meson SoC(include GX/GXL/GXBB, Meson6,8) Fixes: 211ed630753d ("pwm: Add support for Meson PWM Controller") Signed-off-by: Jian Hu Signed-off-by: Yixun Lan --- drivers/pwm/pwm-meson.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/drivers/pwm/pwm-meson.c b/drivers/pwm/pwm-meson.c index d589331d1884..78d9b8c1a4bc 100644 --- a/drivers/pwm/pwm-meson.c +++ b/drivers/pwm/pwm-meson.c @@ -193,6 +193,11 @@ static int meson_pwm_calc(struct meson_pwm *meson, break; } + if (cnt < 2) { + dev_err(meson->chip.dev, "invalid period\n"); + return -EINVAL; + } + if (pre_div == MISC_CLK_DIV_MASK) { dev_err(meson->chip.dev, "unable to get period pre_div\n"); return -EINVAL; @@ -201,19 +206,23 @@ static int meson_pwm_calc(struct meson_pwm *meson, dev_dbg(meson->chip.dev, "period=%u pre_div=%u cnt=%u\n", period, pre_div, cnt); + /* + * Due to the design of hardware, values of 'hi', 'lo' are 1 based + * which mean the actual output from hardware is 'hi' + 1, 'lo' + 1 + */ if (duty == period) { channel->pre_div = pre_div; - channel->hi = cnt; + channel->hi = cnt - 1; channel->lo = 0; } else if (duty == 0) { channel->pre_div = pre_div; channel->hi = 0; - channel->lo = cnt; + channel->lo = cnt - 1; } else { /* Then check is we can have the duty with the same pre_div */ duty_cnt = DIV_ROUND_CLOSEST_ULL((u64)duty * 1000, fin_ps * (pre_div + 1)); - if (duty_cnt > 0xffff) { + if (duty_cnt > 0xffff || !duty_cnt) { dev_err(meson->chip.dev, "unable to get duty cycle\n"); return -EINVAL; } @@ -222,8 +231,8 @@ static int meson_pwm_calc(struct meson_pwm *meson, duty, pre_div, duty_cnt); channel->pre_div = pre_div; - channel->hi = duty_cnt; - channel->lo = cnt - duty_cnt; + channel->hi = duty_cnt - 1; + channel->lo = cnt - duty_cnt - 1; } return 0;