Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp1957773rdf; Sun, 5 Nov 2023 23:27:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IGyqSDSV2jhr0NaNz4raemtCWux4QEjApwRJHu5NslW10HOejmnW2X9mvdy9gz5tCuciYqW X-Received: by 2002:a05:6a21:9993:b0:17a:fe0a:c66c with SMTP id ve19-20020a056a21999300b0017afe0ac66cmr14005702pzb.2.1699255631737; Sun, 05 Nov 2023 23:27:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699255631; cv=none; d=google.com; s=arc-20160816; b=mMtAzEJGHorYJEZJroVhjTi38UeDO/OAXoAneovkU9oCrnQ/bXOf5WmFDQkrU4iQQc aGQhxdgCEXAL6saeecTrfMq9/5koXFgRsgUvTRgqHPKFKZKyxdjKhSNVgehCm9LKAfom wSrvGo/ZYDmpvp7LAvDMhX1/281DXXAFsD/L646kch3IJNhYeIIjlXVaUaDQesBPcig+ /h5oZvQeohdMlY+mLjwi/M2fEXsIP3/jc2pZHucRRZbBdMI49gZDQcNuSS8hUMeodM4o aTQlAK89KiOwk4cIF4096p+yE7Jp7w7s7UQqgU+Iaf12Dfkm0JYJD3dknfJ5UurP4r1s yk3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id; bh=CR5HPyAoBN336U9puUUCXiD3wC+DvGhB9pP0wN96MbQ=; fh=Rpf6snj2sYjYwjj0Br6Z7z/bxVEA8ThtOmysHlDOG5Q=; b=s7wGrMNnDpKtGYF72EgDzcAA9e3N2avDHkJUmqVGp+dALKIJNSnsUi8BCcLWVjwGgN TglqxrNYkWb4nck16M4ZXLp/lSp3920ieCCjFWWpJKafitU7e/cKWqHj+ReHLh8O8a0t wwO/r3FwXEz/tv0jGQka8oVe3a246ha78CbnEgPFCbYsKM3WhgGZRpn/kVlaWJMRckZn Pd5PX409dhjcbLQWme5RshK3YaZOzzlFHxm42iHPZQ4taPGlEiWF6NOmamNgN4uC1UQb w7Hmu0urgeQr7mwSv9p3ZHbB9Gc/+gGZyillIsjEoq78sfr5H95aqey17OXC6LywKDVq LMLA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id h191-20020a636cc8000000b00565db2812a0si7412519pgc.60.2023.11.05.23.27.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Nov 2023 23:27:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id 60BDF804BDE2; Sun, 5 Nov 2023 23:27:09 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230489AbjKFH1C convert rfc822-to-8bit (ORCPT + 99 others); Mon, 6 Nov 2023 02:27:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229841AbjKFH1B (ORCPT ); Mon, 6 Nov 2023 02:27:01 -0500 Received: from fd01.gateway.ufhost.com (fd01.gateway.ufhost.com [61.152.239.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2183C112; Sun, 5 Nov 2023 23:26:57 -0800 (PST) Received: from EXMBX165.cuchost.com (unknown [175.102.18.54]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "EXMBX165", Issuer "EXMBX165" (not verified)) by fd01.gateway.ufhost.com (Postfix) with ESMTP id BC38A24DE73; Mon, 6 Nov 2023 15:26:48 +0800 (CST) Received: from EXMBX168.cuchost.com (172.16.6.78) by EXMBX165.cuchost.com (172.16.6.75) with Microsoft SMTP Server (TLS) id 15.0.1497.42; Mon, 6 Nov 2023 15:26:48 +0800 Received: from [192.168.120.47] (171.223.208.138) by EXMBX168.cuchost.com (172.16.6.78) with Microsoft SMTP Server (TLS) id 15.0.1497.42; Mon, 6 Nov 2023 15:26:47 +0800 Message-ID: <6b57eb4b-241f-4530-b639-5a2e2c55cfd3@starfivetech.com> Date: Mon, 6 Nov 2023 15:26:45 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v6 2/4] pwm: opencores: Add PWM driver support To: =?UTF-8?Q?Uwe_Kleine-K=C3=B6nig?= CC: , , , , "Emil Renner Berthing" , Rob Herring , Thierry Reding , Philipp Zabel , "Krzysztof Kozlowski" , Conor Dooley , Hal Feng , Paul Walmsley , Palmer Dabbelt , Albert Ou 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> <20231102113016.jgsh7jru6vjv4vsp@pengutronix.de> Content-Language: en-US From: William Qiu In-Reply-To: <20231102113016.jgsh7jru6vjv4vsp@pengutronix.de> Content-Type: text/plain; charset="UTF-8" X-Originating-IP: [171.223.208.138] X-ClientProxiedBy: EXCAS066.cuchost.com (172.16.6.26) To EXMBX168.cuchost.com (172.16.6.78) X-YovoleRuleAgent: yovoleflag Content-Transfer-Encoding: 8BIT 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 autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.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 (lipwig.vger.email [0.0.0.0]); Sun, 05 Nov 2023 23:27:09 -0800 (PST) On 2023/11/2 19:30, Uwe Kleine-König wrote: > Hello William, > > On Wed, Nov 01, 2023 at 10:22:44AM +0800, William Qiu wrote: >> >> >> On 2023/10/20 19:25, Uwe Kleine-König wrote: >> >> + void __iomem *base = 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 = readl(REG_OCPWM_LRC(base)); >> >> + duty_data = readl(REG_OCPWM_HRC(base)); >> >> + ctrl_data = readl(REG_OCPWM_CTRL(base)); >> >> + >> >> + state->period = DIV_ROUND_CLOSEST_ULL((u64)period_data * NSEC_PER_SEC, pwm->clk_rate); >> >> + state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_data * NSEC_PER_SEC, pwm->clk_rate); >> > >> > Please test your driver with PWM_DEBUG enabled. The rounding is wrong >> > here. >> >> 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 ∘ .get_state idempotent > .get_state must round up to match. > > Assuming a clkrate of 49500000 Hz the actual period for REG_OCPWM_LRC = > 400 is 8080.808ns and for REG_OCPWM_LRC = 401 is 8101.010. > > So with REG_OCPWM_LRC = 401 .get_state should report state.period = 8102 > [ns] because if you call .apply with .period = 8101 [ns] you're supposed > to use REG_OCPWM_LRC = 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 > .apply_state(period=21) .get_state() returns period=21. > > Best regards > Uwe > I see, then we'll use DIV_ROUND_DOWN_ULL for .apply() and DIV_ROUND_UP_ULL for .get_state(). Thank you for your answer. Best regards, William