Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp3463118ybl; Mon, 12 Aug 2019 00:30:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqyFLXcvFT5iMeyiqopbCFFajTBCU6e0bIZf//dUGNKq9duq9x2fvs2Yt2HItxlqOkkXJTz2 X-Received: by 2002:a17:90a:2525:: with SMTP id j34mr22668551pje.11.1565595025031; Mon, 12 Aug 2019 00:30:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565595025; cv=none; d=google.com; s=arc-20160816; b=ODK7cBnsU5bgOyT9G5F1QJAbyOAMu9mNshwPAG1L1QqGtTL817X15q7Ny7wmJ3gTvw SBd8mfQ4YIC5G2sldX3koDxCpUAw/tVFLxSX+R6dsutCn6TMn/rbhrNCAgrpOEwXfQ2i HNs/GMDXAUsZc760zx39zQHS3fIdpFr17tlaR92vZKVW3OFFE+cCrUMBqdlYXAmcdB4+ PQlTZHr0bWfZxXSQYNjLIGJJ9mOJDX/LQ2YTY28Fqlsv+ZOTobUDkeZqiCPn1TWLzqjf YvguPStM7v5GWHHVJFcVApuCCSdq4ImhybghxQqO2SHlG0xdE7f2sp2v8Y44pcoeAMBf y4qw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=a5aTkNyNQpnTDblbg7EX7YSfJ3L9MslqQrpJziWh5Tc=; b=X5iNdxQ8WF27LwF0Ha/ejvbnXo6OlEIlXRz+vNzrTQa9hRTNPWORvEYk2aejFWhPs1 RzIGBtSU+cI00YIVk/TwVtEaR/bCshvFBKlqEQFGDATwGIVZQ6qvQzdT5uEu2vR/fp+A qH8nlpwGK9BRoWCx2d/IJyPbLL6nfxvaMBsgdFCac+cvOLFAR067vfo085BvNUo3swq2 FXryXVoBWupuZm/ECX+zWMbztl9kiSgvjk1H6G2yZTTUq2mWaHCQ6O8vJcPsJKnXo8NG 2FUSHMh1Aguhe2vAETwHdIvTmkc3GTa/aD0rpcD/9RCJ0K1R/7voxRf8aXWTd8caTfdF Tzjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="Ve/gw486"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k12si3016440plt.92.2019.08.12.00.30.09; Mon, 12 Aug 2019 00:30:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="Ve/gw486"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727137AbfHLH3X (ORCPT + 99 others); Mon, 12 Aug 2019 03:29:23 -0400 Received: from mail-ot1-f68.google.com ([209.85.210.68]:36201 "EHLO mail-ot1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727104AbfHLH3V (ORCPT ); Mon, 12 Aug 2019 03:29:21 -0400 Received: by mail-ot1-f68.google.com with SMTP id k18so23356421otr.3 for ; Mon, 12 Aug 2019 00:29:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=a5aTkNyNQpnTDblbg7EX7YSfJ3L9MslqQrpJziWh5Tc=; b=Ve/gw486vDcHiLqJx8sVIKEa5aF0Cz0pUACQeRj1uGg65ADuTT92pXhepEAZdPaJy5 Ktp0OXXWcUgo8id91YutizXIMcnzyx+TzZna0CAypokpRz1+h+wHGQz8Gl4gc/L2kqau 99TBcCVqd+G1BaYPNvLN8TKqXCkoJn4BYeiengs/SOtuCOKuyToEbU+9G75qHogl/iqb Gyqk8z3FspIohFazaXX5aiw8YL+P9Pp6I6OaxueDqyhO08xQR8IbbYxdSRCeBnTm+BYu ZoZ1v7EiGY1kdNCwTyK+7q/MD9MPqFvYK8xzhEBdQn4i3fi0t8ol5bpruC5/AZR132zG ko1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=a5aTkNyNQpnTDblbg7EX7YSfJ3L9MslqQrpJziWh5Tc=; b=gK2TiB/QMKGwtFalEPEVUyR/+FpEbrk9e3Z/yaFD/v/GJcOuUJhikpexPgbep/q+MP 2k77TY30EzZh7TGBQREsTXbTIGQz42M12Z0/PrcKOtTBMfiwRH93ThUQVN2JaIIszeJg i9CDls6u4tsZtRVWA2uACTidXdWPVa8OcTZXOcs2T9gBdCdfbxYbpS3cXX2gqykTU59e jFxy1oAw1pmDM1htOnYHVKIGW2UeZy0LYUGp4mfWEChQLfljYtsgdmzQyX1Cl7PDDVbq Z2wRyT6fUkBS9kS9V5c2O0CN27i7YuwG19usPDjmAZO3Zj15nkMdtrZFmGw8GUGeFIU7 4xIA== X-Gm-Message-State: APjAAAXWm4bYlVL6mJ5//VTOA+uHwcr+I7T6HrRMurYQeR0hodlimvyT ZipH7JKFQOaMyzn7aOtwZR52sivQ6fX58bdWs9MSvA== X-Received: by 2002:aca:6056:: with SMTP id u83mr14078528oib.27.1565594959591; Mon, 12 Aug 2019 00:29:19 -0700 (PDT) MIME-Version: 1.0 References: <6a38a3655bc8100764d85cb04dea5c2546a311e1.1565168564.git.baolin.wang@linaro.org> <40127356a1acd1f2ff1be1d8a120b305a4e17af4.1565168564.git.baolin.wang@linaro.org> <20190809091013.vguj4wty7qiab64t@pengutronix.de> <20190809144124.3as3rtctlywxkudr@pengutronix.de> In-Reply-To: <20190809144124.3as3rtctlywxkudr@pengutronix.de> From: Baolin Wang Date: Mon, 12 Aug 2019 15:29:07 +0800 Message-ID: Subject: Re: [PATCH 2/2] pwm: sprd: Add Spreadtrum PWM support To: =?UTF-8?Q?Uwe_Kleine=2DK=C3=B6nig?= Cc: Thierry Reding , Rob Herring , Mark Rutland , Orson Zhai , Chunyan Zhang , Vincent Guittot , linux-pwm@vger.kernel.org, DTML , LKML , kernel@pengutronix.de Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Uwe, On Fri, 9 Aug 2019 at 22:41, Uwe Kleine-K=C3=B6nig wrote: > > Hello Baolin, > > On Fri, Aug 09, 2019 at 06:06:21PM +0800, Baolin Wang wrote: > > On Fri, 9 Aug 2019 at 17:10, Uwe Kleine-K=C3=B6nig > > wrote: > > > On Thu, Aug 08, 2019 at 04:59:39PM +0800, Baolin Wang wrote: > > > > +{ > > > > + struct sprd_pwm_chip *spc =3D > > > > + container_of(chip, struct sprd_pwm_chip, chip); > > > > + struct sprd_pwm_chn *chn =3D &spc->chn[pwm->hwpwm]; > > > > + u64 div, tmp; > > > > + u32 prescale, duty; > > > > + int ret; > > > > + > > > > + /* > > > > + * NOTE: the clocks to PWM channel has to be enabled first be= fore > > > > + * writing to the registers. > > > > + */ > > > > + if (!chn->clk_enabled) { > > > > + ret =3D clk_bulk_prepare_enable(SPRD_PWM_NUM_CLKS, ch= n->clks); > > > > > > Do you really need to enable all 8 clocks to configure a single chann= el? > > > > We have 4 channels, and each channel use 2 clocks, so here only enable > > 2 clocks, see SPRD_PWM_NUM_CLKS. > > Ah, I thought SPRD_PWM_NUM_CLKS was 8. > > > > > + if (ret) { > > > > + dev_err(spc->dev, "failed to enable pwm%u clo= ck\n", > > > > + pwm->hwpwm); > > > > + return ret; > > > > + } > > > > + > > > > + chn->clk_enabled =3D true; > > > > + } > > > > + > > > > + duty =3D duty_ns * SPRD_PWM_MOD_MAX / period_ns; > > > > > > @Baolin: until we're there that there are framework requirements how = to > > > round, please document at least how you do it here. Also describing t= he > > > underlying concepts would be good for the driver reader. > > > > > > Something like: > > > > > > /* > > > * The hardware provides a counter that is feed by the source clock. > > > * The period length is (PRESCALE + 1) * MOD counter steps. > > > * The duty cycle length is (PRESCALE + 1) * DUTY counter steps. > > > * > > > * To keep the maths simple we're always using MOD =3D MOD_MAX. > > > * The value for PRESCALE is selected such that the resulting period > > > * gets the maximal length not bigger than the requested one with the > > > * given settings (MOD =3D MOD_MAX and input clock). > > > */ > > > > Yes, totally agree with you. I will add some documentation for our > > controller's setting. > > > > > > > > @Thierry: having a framework guideline here would be good. Or still > > > better a guideline and a debug setting that notices drivers stepping = out > > > of line. > > > > > > I assume using MOD =3D 0 is forbidden? > > > > > > > + /* > > > > + * According to the datasheet, the period_ns calculation form= ula > > > > + * should be: > > > > + * period_ns =3D 10^9 * (prescale + 1) * mod / clk_rate > > > > + * > > > > + * Then we can get the prescale formula: > > > > + * prescale =3D (period_ns * clk_rate) / (10^9 * mod) -1 > > > > + */ > > > > + tmp =3D chn->clk_rate * period_ns; > > > > + div =3D 1000000000ULL * SPRD_PWM_MOD_MAX; > > > > > > Please use NSEC_PER_SEC instead of 1000000000ULL. > > > > Sure. > > > > > > > > > + prescale =3D div64_u64(tmp, div) - 1; > > > > > > If tmp is smaller than div you end up with prescale =3D 0xffffffff wh= ich > > > should be catched. What if prescale > 0xffff? > > > > Usually we can not meet this case, but you are right, the prescale has > > a limit according to the register definition. So will add a validation > > here. > > > > > > > > > + sprd_pwm_write(spc, pwm->hwpwm, SPRD_PWM_MOD, SPRD_PWM_MOD_MA= X); > > > > + sprd_pwm_write(spc, pwm->hwpwm, SPRD_PWM_DUTY, duty); > > > > > > You're losing precision here as you always use SPRD_PWM_MOD_MAX, righ= t? > > > (Just for my understanding, if this simpler approach is good enough > > > here that's fine.) > > > > Yes, SPRD_PWM_MOD_MAX is good enough. > > ok. > > > > > > > > + sprd_pwm_write(spc, pwm->hwpwm, SPRD_PWM_PAT_LOW, SPRD_PWM_RE= G_MSK); > > > > + sprd_pwm_write(spc, pwm->hwpwm, SPRD_PWM_PAT_HIGH, SPRD_PWM_R= EG_MSK); > > > > > > Please describe these two registers in a short comment. > > > > Sure. > > > > > > > > > + sprd_pwm_write(spc, pwm->hwpwm, SPRD_PWM_PRESCALE, prescale); > > > > > > Is the configuration here atomic in the sense that the write of DUTY > > > above only gets effective when PRESCALE is written? If not, please ad= d > > > > Yes. > > > > > a describing paragraph at the top of the driver similar to what is > > > written in pwm-sifive.c. When the PWM is already running before, how > > > does a configuration change effects the output? Is the currently runn= ing > > > period completed? > > > > Anyway, I'll add some comments to explain how it works. > > I'd apreciate if you'd stick to the format in pwm-sifive.c to make it > easier to grep for that. OK. > > > > > +static void sprd_pwm_get_state(struct pwm_chip *chip, struct pwm_d= evice *pwm, > > > > + struct pwm_state *state) > > > > +{ > > > > + struct sprd_pwm_chip *spc =3D > > > > + container_of(chip, struct sprd_pwm_chip, chip); > > > > + struct sprd_pwm_chn *chn =3D &spc->chn[pwm->hwpwm]; > > > > + u32 enabled, duty, prescale; > > > > + u64 tmp; > > > > + int ret; > > > > + > > > > + ret =3D clk_bulk_prepare_enable(SPRD_PWM_NUM_CLKS, chn->clks)= ; > > > > + if (ret) { > > > > + dev_err(spc->dev, "failed to enable pwm%u clocks\n", > > > > + pwm->hwpwm); > > > > + return; > > > > + } > > > > + > > > > + chn->clk_enabled =3D true; > > > > + > > > > + duty =3D sprd_pwm_read(spc, pwm->hwpwm, SPRD_PWM_DUTY) & SPRD= _PWM_REG_MSK; > > > > + prescale =3D sprd_pwm_read(spc, pwm->hwpwm, SPRD_PWM_PRESCALE= ) & SPRD_PWM_REG_MSK; > > > > + enabled =3D sprd_pwm_read(spc, pwm->hwpwm, SPRD_PWM_ENABLE) &= SPRD_PWM_ENABLE_BIT; > > > > + > > > > + /* > > > > + * According to the datasheet, the period_ns and duty_ns calc= ulation > > > > + * formula should be: > > > > + * period_ns =3D 10^9 * (prescale + 1) * mod / clk_rate > > > > + * duty_ns =3D 10^9 * (prescale + 1) * duty / clk_rate > > > > + */ > > > > + tmp =3D (prescale + 1) * 1000000000ULL * SPRD_PWM_MOD_MAX; > > > > + state->period =3D div64_u64(tmp, chn->clk_rate); > > > > > > This is not idempotent. If you apply the configuration that is return= ed > > > here this shouldn't result in a reconfiguration. > > > > Since we may configure the PWM in bootloader, so in kernel part we > > should get current PWM state to avoid reconfiguration if state > > configuration are same. > > This is also important as some consumers might do something like: > > state =3D pwm_get_state(mypwm) > if (something): > state->duty =3D 0 > else: > state->duty =3D state->period / 2 > pwm_set_state(mypwm, state) > > and then period shouldn't get smaller in each step. > (This won't happen as of now because the PWM framework caches the last > state that was set and returns this for pwm_get_state. Still getting > this right would be good.) I understood your concern, but the period can be configured in bootloader, we have no software things to save the accurate period. Moreover I think I can change to use DIV_ROUND_CLOSET_ULL to keep the accuracy. > > > > > + tmp =3D (prescale + 1) * 1000000000ULL * duty; > > > > + state->duty_cycle =3D div64_u64(tmp, chn->clk_rate); > > > > + > > > > + state->enabled =3D !!enabled; > > > > + > > > > + /* Disable PWM clocks if the PWM channel is not in enable sta= te. */ > > > > + if (!enabled) { > > > > + clk_bulk_disable_unprepare(SPRD_PWM_NUM_CLKS, chn->cl= ks); > > > > + chn->clk_enabled =3D false; > > > > + } > > > > +} > > > > + > > > > +static const struct pwm_ops sprd_pwm_ops =3D { > > > > + .config =3D sprd_pwm_config, > > > > + .enable =3D sprd_pwm_enable, > > > > + .disable =3D sprd_pwm_disable, > > > > + .get_state =3D sprd_pwm_get_state, > > > > + .owner =3D THIS_MODULE, > > > > +}; > > > > + > > > > +static int sprd_pwm_clk_init(struct sprd_pwm_chip *spc) > > > > +{ > > > > + struct clk *clk_parent, *clk_pwm; > > > > + int ret, i, clk_index =3D 0; > > > > + > > > > + clk_parent =3D devm_clk_get(spc->dev, "source"); > > > > + if (IS_ERR(clk_parent)) { > > > > + dev_err(spc->dev, "failed to get source clock\n"); > > > > + return PTR_ERR(clk_parent); > > > > + } > > > > + > > > > + for (i =3D 0; i < SPRD_PWM_NUM; i++) { > > > > + struct sprd_pwm_chn *chn =3D &spc->chn[i]; > > > > + int j; > > > > + > > > > + for (j =3D 0; j < SPRD_PWM_NUM_CLKS; ++j) > > > > + chn->clks[j].id =3D sprd_pwm_clks[clk_index++= ]; > > > > + > > > > + ret =3D devm_clk_bulk_get(spc->dev, SPRD_PWM_NUM_CLKS= , chn->clks); > > > > + if (ret) { > > > > + if (ret =3D=3D -ENOENT) > > > > + break; > > > > > > devm_clk_bulk_get_optional ? I'm sure you don't want to get all 8 clo= cks > > > 8 times. > > > > This is not optional, each channel has 2 required clocks, and we have > > 4 channels. (SPRD_PWM_NUM_CLKS =3D=3D 2) > > I see. Currently it is not possible to use channel 2 if there are no > clocks for channel 0, right? There is no hardware related problem here, Yes. > just a shortcoming of the driver? Yes. > > > > > + > > > > + dev_err(spc->dev, "failed to get channel cloc= ks\n"); > > > > + return ret; > > > > + } > > > > + > > > > + clk_pwm =3D chn->clks[1].clk; > > > > > > This 1 looks suspicious. Are you using all clocks provided in the dtb= at > > > all? You're not using i in the loop at all, this doesn't look right. > > > > Like I said above, each channel has 2 clocks: enable clock and pwm > > clock, the 2nd clock of each channel's bulk clocks is the pwm clock, > > which is used to set the source clock. I know this's not easy to read, > > so do you have any good suggestion? > > Not sure this is easily possible to rework to make this clearer. > > Do these clks have different uses? e.g. one to enable register access > and the other to enable the pwm output? If so just using Yes. > devm_clk_bulk_get isn't the right thing because you should be able know > if clks[0] or clks[1] is the one you need to enable the output (or > register access). We've fixed the clock order in bulk clocks by the array 'sprd_pwm_clks', maybe I should define one readable macro instead of magic number. > > > > > + if (!clk_set_parent(clk_pwm, clk_parent)) > > > > + chn->clk_rate =3D clk_get_rate(clk_pwm); > > > > + else > > > > + chn->clk_rate =3D SPRD_PWM_DEFAULT_CLK; > > > > > > I don't know all the clock framework details, but I think there are > > > better ways to ensure that a given clock is used as parent for anothe= r > > > given clock. Please read the chapter about "Assigned clock parents an= d > > > rates" in the clock bindings and check if this could be used for the > > > purpose here and so simplify the driver. > > > > Actually there are many other drivers set the parent clock like this, > > and we want a default clock if failed to set the parent clock. > > These might be older than the clk framework capabilities, or the > reviewers didn't pay attention to this detail; both shouldn't be a > reason to not make it better here. The clock framework supplies 'assigned-clocks' and 'assigned-clock-parents' properties to set parent, but for our case we still want to set a default clock rate if failed to set parent when met some abnormal things. > > > > > + return 0; > > > > +} > > > > + > > > > +static int sprd_pwm_remove(struct platform_device *pdev) > > > > +{ > > > > + struct sprd_pwm_chip *spc =3D platform_get_drvdata(pdev); > > > > + int i; > > > > + > > > > + for (i =3D 0; i < spc->num_pwms; i++) > > > > + pwm_disable(&spc->chip.pwms[i]); > > > > > > This is wrong. You must not call pwm_disable here. It's the consumer'= s > > > responsibility to disable the hardware. > > > > Emm, I saw some drivers did like this, like pwm-spear.c. > > Could you elaborate on what's the problem if the driver issues pwm_disa= ble? > > This is a function to be called from code that called pwm_get before > (i.e. pwm consumers). This just doesn't explode because up to now the > PWM framework is only a thin wrapper around the lowlevel driver > callbacks. > > The reasoning is similar to what I wrote in commit > f82d15e223403b05fffb33ba792b87a8620f6fee. I'd like to have a PWM_DEBUG > setting that catches such problems but the discussion with Thierry to > even document the expectations is stuck, see > https://patchwork.ozlabs.org/patch/1021566/. > Thanks for your explanation, and I'll remove pwm_disable from driver. Thanks for your comments. --=20 Baolin Wang Best Regards