Received: by 2002:a05:7412:518d:b0:e2:908c:2ebd with SMTP id fn13csp412672rdb; Thu, 5 Oct 2023 09:21:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEi2j/VxKesJxqX9xqs5cNg58FcrnbUVekdOhpredUv5WvUBdMrauS6wM5d2h/1YZKaK26l X-Received: by 2002:a05:6a00:1403:b0:68f:a92a:8509 with SMTP id l3-20020a056a00140300b0068fa92a8509mr2524190pfu.7.1696522900641; Thu, 05 Oct 2023 09:21:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696522900; cv=none; d=google.com; s=arc-20160816; b=eLY7W37f/J8YVbHt2z3sebAP0fiRsLvOwCUGXRh4hzb1ZGK2D/QwPdlHPWsysSZ3Ln Ku3aSbM1DaS0LGyVYZn4FT8aeiwcy5gaU2DBYTOw2j190WbhLj4IMnJrn55Es1Tq9/n3 r7a0Ybh8m3wsTnlzOdEkAQfu2lHOaMaMZw7IlckRqLMIAt7ErSOHSn22i+rRjSVEpY/v 9N97aelQZVOjI+ZJ7jm/DGG8glwvn26Z5/Xt4zRRMEmb5oGaStdSFHxw4s00vCDtY2BN kQ1U9BETnkOyZlKDxJJlhPjTkXVK0hdOo8olO9fYs7/kMrdOe90I2MDfYN0II9jGMgl/ muaQ== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=ElPNb9avn+BuVOoFxaOvHZYPJeSQoEJMAnFeztcAtuw=; fh=DgNBKz4Mg3XIWBIgdC6yEkEEvWKsc+hqPaOgPOaY+iE=; b=UPUBiis6dceFw6MUmGk7ZgD8dXi4ptKl4alu+B34gl+8rAAWgfWkvF0yrYlzJ5mzbi wV6oeoBTJ3pzhy8PIatJmIsBQsqZye2ap+ubd6XwMfS+NrED2M5e/L8k65AQnR5aWhKz sklm80KdJPcYC6DJoyXbCArQI0rlClZKLM1OjLfUvQ4NQDKFvlNmAbmFNFAYij42fQBC +DEtVUSNQmMNPJKa37r045YSnrA1xlP6CZpOBXcJmSpgnTzxynfJa3c6GaW0BWJ0J8Kk AJrf2MzaPRyz/Ap4mYC8gdGjWVIy8vKjkil+l2jUNtpdSKoJbWYTToMpn9lkhx1Twdmx VO5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FZq+rtBH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id x9-20020a056a00188900b0068fadbc3169si1808413pfh.254.2023.10.05.09.21.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 09:21:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FZq+rtBH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id CEA1C806FA83; Thu, 5 Oct 2023 09:21:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242759AbjJEQTB (ORCPT + 99 others); Thu, 5 Oct 2023 12:19:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35390 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235430AbjJEQPb (ORCPT ); Thu, 5 Oct 2023 12:15:31 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80302AFB9; Thu, 5 Oct 2023 07:18:59 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4E33CC43395; Thu, 5 Oct 2023 14:18:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696515539; bh=+5giS8dCDEB9SSIrIST7R8CXKZKXlmVab1T0e3RZ980=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=FZq+rtBHFgu7hUs9WU6oYZctlBrd8N8B6gpDD6vXr/eRZuuDq5yhSI/zvykHn5bDG FxdBPitqVLrJMSY2mQ5UEB8SQR+q42ZA328/UwbpxuQg8xFE4s8cHoyPLZyCFlaJm2 T8oZPiVBbWLi7I528nPlTqYqOiYM4XSlFSJe6B5tMJpFlK9QpdxD+TldHQwf7bibC2 sU0eKnKWL9hjEdZ4CKwA+7bpCQwxA/GxAj6ro2nfoSowaAJb9MLyufNioMejjt0SV6 GvMsnjHqOYXyPXDbzQ4TO6ni2SPaiSB2w3FFb523TufPFH1DyA58ff5TAH+ONFDlVh j/hSOIbqc4dUg== Date: Thu, 5 Oct 2023 22:06:50 +0800 From: Jisheng Zhang To: Uwe =?utf-8?Q?Kleine-K=C3=B6nig?= Cc: Thierry Reding , Rob Herring , Krzysztof Kozlowski , Conor Dooley , linux-pwm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Subject: Re: [PATCH v2 2/2] pwm: add T-HEAD PWM driver Message-ID: References: <20231004092731.1362-1-jszhang@kernel.org> <20231004092731.1362-3-jszhang@kernel.org> <20231004140130.ljsfpn4axmsmszwm@pengutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20231004140130.ljsfpn4axmsmszwm@pengutronix.de> X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Thu, 05 Oct 2023 09:21:18 -0700 (PDT) On Wed, Oct 04, 2023 at 04:01:30PM +0200, Uwe Kleine-König wrote: > On Wed, Oct 04, 2023 at 05:27:31PM +0800, Jisheng Zhang wrote: > > T-HEAD SoCs such as the TH1520 contain a PWM controller used > > to control the LCD backlight, fan and so on. Add driver for it. > > > > Signed-off-by: Jisheng Zhang > > --- ... Hi Uwe, Thanks a lot for your review and nice suggestions. v3 has been sent out. And I want to add more comments to your questions here. > > + > > +static int thead_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, > > + const struct pwm_state *state) > > +{ > > + struct thead_pwm_chip *priv = thead_pwm_from_chip(chip); > > + u64 period_cycle, duty_cycle, rate; > > + u32 val; > > + > > + /* if ever started, can't change the polarity */ > > + if (priv->ever_started && state->polarity != pwm->state.polarity) > > + return -EINVAL; This is the polority check[1] for ever started channel. > > + > > + if (!state->enabled) { > > + if (pwm->state.enabled) { > > + val = readl(priv->mmio_base + THEAD_PWM_CTRL(pwm->hwpwm)); > > + val &= ~THEAD_PWM_CFG_UPDATE; > > + writel(val, priv->mmio_base + THEAD_PWM_CTRL(pwm->hwpwm)); > > + > > + writel(0, priv->mmio_base + THEAD_PWM_FP(pwm->hwpwm)); > > + > > + val |= THEAD_PWM_CFG_UPDATE; > > + writel(val, priv->mmio_base + THEAD_PWM_CTRL(pwm->hwpwm)); > > + } > > + return 0; > > + } > > + > > + if (!pwm->state.enabled) > > + pm_runtime_get_sync(chip->dev); > > pm_runtime_get_sync() returns an int that you shouldn't ignore. In v3 I switch to pm_runtime_resume_and_get() because it can simplify the error handling code. > > > + val = readl(priv->mmio_base + THEAD_PWM_CTRL(pwm->hwpwm)); > > + val &= ~THEAD_PWM_CFG_UPDATE; > > + > > + if (state->polarity == PWM_POLARITY_INVERSED) > > + val &= ~THEAD_PWM_FPOUT; > > + else > > + val |= THEAD_PWM_FPOUT; > > What happens here if the bootloader already touched that flag? Or the > driver is reloaded/rebound? Only polarity can't be changed once started, so if bootloader already configured polarity and started the pwm channel, and we want to change to a different polarity, the check[1] in the beginning of this function will fail so return -EINVAL. > > > + writel(val, priv->mmio_base + THEAD_PWM_CTRL(pwm->hwpwm)); > > + > > + rate = clk_get_rate(priv->clk); > > + /* > > + * The following calculations might overflow if clk is bigger > > + * than 1 GHz. In practise it's 24MHz, so this limitation > > + * is only theoretic. > > + */ > > + if (rate > (u64)NSEC_PER_SEC) > > this cast isn't needed. > > > + return -EINVAL; > > + > > + period_cycle = mul_u64_u64_div_u64(rate, state->period, NSEC_PER_SEC); > > + if (period_cycle > THEAD_PWM_MAX_PERIOD) > > + period_cycle = THEAD_PWM_MAX_PERIOD; > > + /* > > + * With limitation above we have period_cycle <= THEAD_PWM_MAX_PERIOD, > > + * so this cannot overflow. > > + */ > > + writel((u32)period_cycle, priv->mmio_base + THEAD_PWM_PER(pwm->hwpwm)); > > This cast can also be dropped. > > > + > > + duty_cycle = mul_u64_u64_div_u64(rate, state->duty_cycle, NSEC_PER_SEC); > > + if (duty_cycle > THEAD_PWM_MAX_DUTY) > > + duty_cycle = THEAD_PWM_MAX_DUTY; > > + /* > > + * With limitation above we have duty_cycle <= THEAD_PWM_MAX_PERIOD, > > + * so this cannot overflow. > > + */ > > + writel((u32)duty_cycle, priv->mmio_base + THEAD_PWM_FP(pwm->hwpwm)); > > ... > > > + > > + val |= THEAD_PWM_CFG_UPDATE; > > + writel(val, priv->mmio_base + THEAD_PWM_CTRL(pwm->hwpwm)); > > + > > + if (!pwm->state.enabled) { > > + val |= THEAD_PWM_START; > > + writel(val, priv->mmio_base + THEAD_PWM_CTRL(pwm->hwpwm)); > > + priv->ever_started = true; > > + } > > Further above you conditionally call pm_runtime_get_sync(), there should > be a matching pm_runtime_put(). In v3, I call pm_runtime_put_sync() when pwm channel is disabled. Thanks