linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Jonathan Neuschäfer" <j.neuschaefer@gmx.net>
To: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
Cc: "Jonathan Neuschäfer" <j.neuschaefer@gmx.net>,
	linux-kernel@vger.kernel.org,
	"Alexandre Belloni" <alexandre.belloni@bootlin.com>,
	"Heiko Stuebner" <heiko@sntech.de>,
	linux-pwm@vger.kernel.org,
	"Linus Walleij" <linus.walleij@linaro.org>,
	"Thierry Reding" <thierry.reding@gmail.com>,
	"Fabio Estevam" <festevam@gmail.com>,
	linux-rtc@vger.kernel.org, "Arnd Bergmann" <arnd@arndb.de>,
	"Mauro Carvalho Chehab" <mchehab+huawei@kernel.org>,
	"Sam Ravnborg" <sam@ravnborg.org>,
	"Daniel Palmer" <daniel@0x0f.com>,
	"Andy Shevchenko" <andy.shevchenko@gmail.com>,
	"Andreas Kemnade" <andreas@kemnade.info>,
	"NXP Linux Team" <linux-imx@nxp.com>,
	devicetree@vger.kernel.org,
	"Stephan Gerhold" <stephan@gerhold.net>,
	allen <allen.chen@ite.com.tw>,
	"Sascha Hauer" <s.hauer@pengutronix.de>,
	"Lubomir Rintel" <lkundrak@v3.sk>,
	"Rob Herring" <robh+dt@kernel.org>,
	"Lee Jones" <lee.jones@linaro.org>,
	linux-arm-kernel@lists.infradead.org,
	"Alessandro Zummo" <a.zummo@towertech.it>,
	"Mark Brown" <broonie@kernel.org>,
	"Pengutronix Kernel Team" <kernel@pengutronix.de>,
	"Heiko Stuebner" <heiko.stuebner@theobroma-systems.com>,
	"Josua Mayer" <josua.mayer@jm0.eu>,
	"Shawn Guo" <shawnguo@kernel.org>,
	"David S. Miller" <davem@davemloft.net>
Subject: Re: [PATCH v5 4/7] pwm: ntxec: Add driver for PWM function in Netronix EC
Date: Tue, 1 Dec 2020 14:22:14 +0100	[thread overview]
Message-ID: <20201201132214.GI456020@latitude> (raw)
In-Reply-To: <20201201072026.a736ikf3k4udpvfv@pengutronix.de>

[-- Attachment #1: Type: text/plain, Size: 4090 bytes --]

On Tue, Dec 01, 2020 at 08:20:26AM +0100, Uwe Kleine-König wrote:
> Hello Jonathan,
> 
> very nice driver, just a few minor comments below.
> 
> On Tue, Dec 01, 2020 at 02:15:10AM +0100, Jonathan Neuschäfer wrote:
> > +static struct ntxec_pwm *pwmchip_to_priv(struct pwm_chip *chip)
> 
> a function prefix would be great here, I'd pick ntxec_pwm_from_chip as
> name.

Good point, will do.

> 
> > +{
> > +	return container_of(chip, struct ntxec_pwm, chip);
> > +}
> > +
> > +[...]
> > +static int ntxec_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm_dev,
> > +			   const struct pwm_state *state)
> > +{
> > +	struct ntxec_pwm *priv = pwmchip_to_priv(pwm_dev->chip);
> > +	unsigned int period, duty;
> > +	struct reg_sequence regs[] = {
> > +		{ NTXEC_REG_PERIOD_HIGH },
> > +		{ NTXEC_REG_PERIOD_LOW },
> > +		{ NTXEC_REG_DUTY_HIGH },
> > +		{ NTXEC_REG_DUTY_LOW }
> > +	};
> > +	int res;
> > +
> > +	if (state->polarity != PWM_POLARITY_NORMAL)
> > +		return -EINVAL;
> > +
> > +	period = min_t(u64, state->period, MAX_PERIOD_NS);
> > +	duty   = min_t(u64, state->duty_cycle, period);
> 
> I'm not a big fan of aligning =. (As if you have to add a longer
> variable you have to realign all otherwise unrelated lines.) But that's
> subjective and it's up to you if you want to change this.

In this case, I thought it helps the readability, because the lines are
quite similar.

> > +	period /= TIME_BASE_NS;
> > +	duty   /= TIME_BASE_NS;

Here, I did it because I had already aligned the previous two lines.

> > +
> > +	/*
> > +	 * Changes to the period and duty cycle take effect as soon as the
> > +	 * corresponding low byte is written, so the hardware may be configured
> > +	 * to an inconsistent state after the period is written and before the
> > +	 * duty cycle is fully written. If, in such a case, the old duty cycle
> > +	 * is longer than the new period, the EC may output 100% for a moment.
> > +	 */
> > +
> > +	regs[0].def = ntxec_reg8(period >> 8);
> > +	regs[1].def = ntxec_reg8(period);
> > +	regs[2].def = ntxec_reg8(duty >> 8);
> > +	regs[3].def = ntxec_reg8(duty);
> 
> You could even minimize the window by changing the order here to
> 
> 	NTXEC_REG_PERIOD_HIGH
> 	NTXEC_REG_DUTY_HIGH
> 	NTXEC_REG_PERIOD_LOW
> 	NTXEC_REG_DUTY_LOW

Good idea, but I'm not sure if the EC handles this kind of interleaving
correctly.

> but it gets less readable. Maybe move that to a function to have the
> reg_sequence and the actual write nearer together?

Indeed, a separate function would keep register names and values
together (without resorting to declarations-after-statements).

> Or somehow name the indexes to make it more obvious?

Too much unnecessary complexity, IMHO.

> > +	res = regmap_multi_reg_write(priv->ec->regmap, regs, ARRAY_SIZE(regs));
> > +	if (res)
> > +		return res;
> > +
> > +	/*
> > +	 * Writing a duty cycle of zero puts the device into a state where
> > +	 * writing a higher duty cycle doesn't result in the brightness that it
> > +	 * usually results in. This can be fixed by cycling the ENABLE register.
> > +	 *
> > +	 * As a workaround, write ENABLE=0 when the duty cycle is zero.
> 
> If the device already has duty_cycle = 0 but ENABLE = 1, you might get
> a failure. But I guess this doesn't need addressing in the code. But
> maybe point it out in a comment?

Good point. I'll add something to the comment.

> > +	 */
> > +	if (state->enabled && duty != 0) {
> > +		res = regmap_write(priv->ec->regmap, NTXEC_REG_ENABLE, ntxec_reg8(1));
> > +		if (res)
> > +			return res;
> > +
> > +		/* Disable the auto-off timer */
> > +		res = regmap_write(priv->ec->regmap, NTXEC_REG_AUTO_OFF_HI, ntxec_reg8(0xff));
> > +		if (res)
> > +			return res;
> > +
> > +		return regmap_write(priv->ec->regmap, NTXEC_REG_AUTO_OFF_LO, ntxec_reg8(0xff));
> 
> Given that you cannot read back period and duty anyhow: Does it make
> sense to write these only if (state->enabled && duty != 0)?

I think it does.


Thanks,
Jonathan

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2020-12-01 13:24 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-01  1:15 [PATCH v5 0/7] Netronix embedded controller driver for Kobo and Tolino ebook readers Jonathan Neuschäfer
2020-12-01  1:15 ` [PATCH v5 1/7] dt-bindings: Add vendor prefix for Netronix, Inc Jonathan Neuschäfer
2020-12-01  1:15 ` [PATCH v5 2/7] dt-bindings: mfd: Add binding for Netronix embedded controller Jonathan Neuschäfer
2020-12-01  1:15 ` [PATCH v5 3/7] mfd: Add base driver " Jonathan Neuschäfer
2020-12-01  1:15 ` [PATCH v5 4/7] pwm: ntxec: Add driver for PWM function in Netronix EC Jonathan Neuschäfer
2020-12-01  7:20   ` Uwe Kleine-König
2020-12-01 13:22     ` Jonathan Neuschäfer [this message]
2020-12-01  1:15 ` [PATCH v5 5/7] rtc: New driver for RTC in Netronix embedded controller Jonathan Neuschäfer
2020-12-01  1:15 ` [PATCH v5 6/7] MAINTAINERS: Add entry for " Jonathan Neuschäfer
2020-12-01  1:15 ` [PATCH v5 7/7] ARM: dts: imx50-kobo-aura: Add " Jonathan Neuschäfer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201201132214.GI456020@latitude \
    --to=j.neuschaefer@gmx.net \
    --cc=a.zummo@towertech.it \
    --cc=alexandre.belloni@bootlin.com \
    --cc=allen.chen@ite.com.tw \
    --cc=andreas@kemnade.info \
    --cc=andy.shevchenko@gmail.com \
    --cc=arnd@arndb.de \
    --cc=broonie@kernel.org \
    --cc=daniel@0x0f.com \
    --cc=davem@davemloft.net \
    --cc=devicetree@vger.kernel.org \
    --cc=festevam@gmail.com \
    --cc=heiko.stuebner@theobroma-systems.com \
    --cc=heiko@sntech.de \
    --cc=josua.mayer@jm0.eu \
    --cc=kernel@pengutronix.de \
    --cc=lee.jones@linaro.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-imx@nxp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=lkundrak@v3.sk \
    --cc=mchehab+huawei@kernel.org \
    --cc=robh+dt@kernel.org \
    --cc=s.hauer@pengutronix.de \
    --cc=sam@ravnborg.org \
    --cc=shawnguo@kernel.org \
    --cc=stephan@gerhold.net \
    --cc=thierry.reding@gmail.com \
    --cc=u.kleine-koenig@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).