Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp306331rdb; Thu, 2 Nov 2023 04:31:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHBwSaXaPevXbJUDEmmKZMeSwPzBfmL15eg/qHBLWgbCYuaoIxkZltPyo8bfvymA8lagZlx X-Received: by 2002:a17:907:da1:b0:9be:7b67:1673 with SMTP id go33-20020a1709070da100b009be7b671673mr4588425ejc.1.1698924677604; Thu, 02 Nov 2023 04:31:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698924677; cv=none; d=google.com; s=arc-20160816; b=gWlPBQ1MAP23GbhI6xgBd91lctmxvde10Y8StSUbI+ccWyFF0hwLxcgduo6O5jC2Ft 3E6AIuyPk+zannxpkamI2RPHfOXKm++KZo4ldzXdGsaeYMzW3bncOP8AvlDFlayrquET FufXgZR2LrXxVEwVIJmcoRJZw2E8Zm76OFXU2RMPXdprNZwJAwPrwynggQ3dFw784dtT JawOROl1a3TK3szymz2ykQMKlIWXJOnWjKgb4dOV5Z3YdLPx9JdjU1BcI50Bm1pOc99o gPw9FS0SWtRHrqf5OmlwNLb0SuBvQ/O4klBw//LNXDSlMIRl8Uh9G0GgjW9HlTgP2Qer OMsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=W6AmI9J5Z0SJ9Fmz5ojtPzZ1nIlEo6lGj8SmPnjxZA4=; fh=bLSaacV+JNFxQmNbLDTGg8GHxFdkKXVwBNQB5K8M4gM=; b=DNxmSGkvtwNtQyrNFZMCqzKTf9BZZZaL02gDEdDdVRWgk874MXlwz1mI+XfOQgyHmG KA6Pn5bd/cw3nVZSRXTeABQr5VGt1sNUVS5IwLPOduNrq9gTRGNyRwafKfu81b5VfDnK 55Ho575tGbh5xEWG6bO9FdWsW1ZhrUW173pWG/HPyvGtDM01pon+uJ55OeHhymKBCHDe jYrPGDrsFitHpSQjtiZkO0DDjtxw4C0nlSDGhgcHqPbhtZ5YiQKRI+u8Jjx6vpnJI626 r4b8d4xr0bf3p5JpejeYC3pAqa4i9hu3NWTJzMJuaTGa2aKXfp+8kgmFzFhoWZdnzpeV 7xyA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id he6-20020a1709073d8600b0099d8822862bsi859139ejc.54.2023.11.02.04.30.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 04:31:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id CD54880292A3; Thu, 2 Nov 2023 04:30:48 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347250AbjKBLae (ORCPT + 99 others); Thu, 2 Nov 2023 07:30:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229832AbjKBLac (ORCPT ); Thu, 2 Nov 2023 07:30:32 -0400 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [IPv6:2a0a:edc0:2:b01:1d::104]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85B38111 for ; Thu, 2 Nov 2023 04:30:26 -0700 (PDT) Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qyVtz-0005lO-6l; Thu, 02 Nov 2023 12:30:19 +0100 Received: from [2a0a:edc0:0:900:1d::77] (helo=ptz.office.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qyVtw-0063ea-Mn; Thu, 02 Nov 2023 12:30:16 +0100 Received: from ukl by ptz.office.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1qyVtw-00BVjO-DL; Thu, 02 Nov 2023 12:30:16 +0100 Date: Thu, 2 Nov 2023 12:30:16 +0100 From: Uwe =?utf-8?Q?Kleine-K=C3=B6nig?= To: William Qiu Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-pwm@vger.kernel.org, Emil Renner Berthing , Rob Herring , Thierry Reding , Philipp Zabel , Krzysztof Kozlowski , Conor Dooley , Hal Feng , Paul Walmsley , Palmer Dabbelt , Albert Ou Subject: Re: [PATCH v6 2/4] pwm: opencores: Add PWM driver support Message-ID: <20231102113016.jgsh7jru6vjv4vsp@pengutronix.de> References: <20231020103741.557735-1-william.qiu@starfivetech.com> <20231020103741.557735-3-william.qiu@starfivetech.com> <20231020112539.gctx5uj2rrhryulo@pengutronix.de> <7d64ea1d-b573-4a69-ba0c-4cbfab638c5e@starfivetech.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="ltmjasklp4ngpzku" Content-Disposition: inline In-Reply-To: <7d64ea1d-b573-4a69-ba0c-4cbfab638c5e@starfivetech.com> X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 02 Nov 2023 04:30:49 -0700 (PDT) --ltmjasklp4ngpzku Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hello William, On Wed, Nov 01, 2023 at 10:22:44AM +0800, William Qiu wrote: >=20 >=20 > On 2023/10/20 19:25, Uwe Kleine-K=C3=B6nig wrote: > >> + void __iomem *base =3D pwm->data->get_ch_base ? > >> + pwm->data->get_ch_base(pwm->regs, dev->hwpwm) : pwm->regs; > >> + u32 period_data, duty_data, ctrl_data; > >> + > >> + period_data =3D readl(REG_OCPWM_LRC(base)); > >> + duty_data =3D readl(REG_OCPWM_HRC(base)); > >> + ctrl_data =3D readl(REG_OCPWM_CTRL(base)); > >> + > >> + state->period =3D DIV_ROUND_CLOSEST_ULL((u64)period_data * NSEC_PER_= SEC, pwm->clk_rate); > >> + state->duty_cycle =3D DIV_ROUND_CLOSEST_ULL((u64)duty_data * NSEC_PE= R_SEC, pwm->clk_rate); > >=20 > > Please test your driver with PWM_DEBUG enabled. The rounding is wrong > > here. >=20 > The conclusion after checking is: when the period or duty_cycle value set > by the user is not divisible (1000000000/49.5M), there will be an error. > This error is due to hardware accuracy. So why is rounding is wrong? > rockchip also has a similar implementation drivers/pwm/ pwm-rockchip.c I fail to follow. Where is an error? The general policy (for new drivers at least) is to implement the biggest period possible not bigger than the requested period. That means that .apply must round down and to make .apply =E2=88=98 .get_state idempot= ent =2Eget_state must round up to match. Assuming a clkrate of 49500000 Hz the actual period for REG_OCPWM_LRC =3D 400 is 8080.808ns and for REG_OCPWM_LRC =3D 401 is 8101.010. So with REG_OCPWM_LRC =3D 401 .get_state should report state.period =3D 8102 [ns] because if you call .apply with .period =3D 8101 [ns] you're supposed to use REG_OCPWM_LRC =3D 400. Rounding using DIV_ROUND_CLOSEST doesn't give consistent behaviour in some cases. Consider a PWM that can implement the following periods (and none in between): 20.1 ns 20.4 ns 21.7 ns With round-to-nearest a request to configure 21 ns will yield 20.4 ns. If you call .get_state there the driver will return 20 ns. However configuring 20 ns results in a period of 20.1 ns. With rounding as requested above you get a consistent behaviour. After =2Eapply_state(period=3D21) .get_state() returns period=3D21. Best regards Uwe --=20 Pengutronix e.K. | Uwe Kleine-K=C3=B6nig = | Industrial Linux Solutions | https://www.pengutronix.de/ | --ltmjasklp4ngpzku Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEEP4GsaTp6HlmJrf7Tj4D7WH0S/k4FAmVDiEcACgkQj4D7WH0S /k5P5Qf/czkHt1isZ7/ZX5Q+xUYicANztUd5HO2YB1j1TaGv02Q8gE7OomO6Nk/8 NYYx0uxugJPlJlUQ5omGaZzFBE0neKAIfZXuKL7OfIOIIL+dusgvYnY2TRGJWIBQ o8z9IyZKGh46aUnC2ZToUYJAYqYAxpr3TkUkPl8xVgSlPvgM8uwWAAxyuJWWR/Pk +rm6Kl2Hb3nZ1tnH1OpV2W8kisHQjtyBR0TWtpjioDSpO7IEp1wlg6ayVPcQQTJe kHkf/l/RUEVqo7D3nzmg17qT8vzzPGjk2kgm42A9ce6jUAcemq0MOIQtLfS7vWjJ vM/+J3PQqV+D4wuWImRyKwZ5chY6Pg== =rI74 -----END PGP SIGNATURE----- --ltmjasklp4ngpzku--