Linux-RTC Archive on lore.kernel.org
 help / color / Atom feed
From: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
To: "Jonathan Neuschäfer" <j.neuschaefer@gmx.net>
Cc: 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,
	Mauro Carvalho Chehab <mchehab+huawei@kernel.org>,
	Sam Ravnborg <sam@ravnborg.org>,
	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: [RFC PATCH 06/10] pwm: ntxec: Add driver for PWM function in Netronix EC
Date: Mon, 22 Jun 2020 10:18:02 +0200
Message-ID: <20200622081802.pv4xmb7vn4te5r5t@taurus.defre.kleine-koenig.org> (raw)
In-Reply-To: <20200620224222.1312520-5-j.neuschaefer@gmx.net>


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

Hello,

On Sun, Jun 21, 2020 at 12:42:17AM +0200, Jonathan Neuschäfer wrote:
> The Netronix EC provides a PWM output, which is used for the backlight

s/,//

> on ebook readers. This patches adds a driver for the PWM output.

on *some* ebook readers


> +#define NTXEC_UNK_A		0xa1
> +#define NTXEC_UNK_B		0xa2
> +#define NTXEC_ENABLE		0xa3
> +#define NTXEC_PERIOD_LOW	0xa4
> +#define NTXEC_PERIOD_HIGH	0xa5
> +#define NTXEC_DUTY_LOW		0xa6
> +#define NTXEC_DUTY_HIGH		0xa7
> +
> +/*
> + * The time base used in the EC is 8MHz, or 125ns. Period and duty cycle are
> + * measured in this unit.
> + */
> +static int ntxec_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm_dev,
> +				 int duty_ns, int period_ns)
> +{
> +	struct ntxec_pwm *pwm = pwmchip_to_pwm(chip);
> +	uint64_t duty = duty_ns;
> +	uint64_t period = period_ns;

As you cannot use values bigger than 8191999 anyhow, I wonder why you
use a 64 bit type here.

> +	int res = 0;
> +
> +	do_div(period, 125);

Please use a define instead of plain 125.

> +	if (period > 0xffff) {
> +		dev_warn(pwm->dev,
> +			 "Period is not representable in 16 bits: %llu\n", period);
> +		return -ERANGE;
> +	}
> +
> +	do_div(duty, 125);
> +	if (duty > 0xffff) {
> +		dev_warn(pwm->dev, "Duty cycle is not representable in 16 bits: %llu\n",
> +			duty);
> +		return -ERANGE;
> +	}

This check isn't necessary as the pwm core ensures that duty <= period.

> +	res |= ntxec_write8(pwm->ec, NTXEC_PERIOD_HIGH, period >> 8);
> +	res |= ntxec_write8(pwm->ec, NTXEC_PERIOD_LOW, period);
> +	res |= ntxec_write8(pwm->ec, NTXEC_DUTY_HIGH, duty >> 8);
> +	res |= ntxec_write8(pwm->ec, NTXEC_DUTY_LOW, duty);

Does this complete the currently running period? Can it happen that a
new period starts between the first and the last write and so a mixed
period can be seen at the output?

> +
> +	return (res < 0) ? -EIO : 0;
> +}
> +
> +static int ntxec_pwm_enable(struct pwm_chip *chip,
> +				 struct pwm_device *pwm_dev)
> +{
> +	struct ntxec_pwm *pwm = pwmchip_to_pwm(chip);
> +
> +	return ntxec_write8(pwm->ec, NTXEC_ENABLE, 1);
> +}
> +
> +static void ntxec_pwm_disable(struct pwm_chip *chip,
> +				   struct pwm_device *pwm_dev)
> +{
> +	struct ntxec_pwm *pwm = pwmchip_to_pwm(chip);
> +
> +	ntxec_write8(pwm->ec, NTXEC_ENABLE, 0);
> +}
> +
> +static struct pwm_ops ntxec_pwm_ops = {
> +	.config		= ntxec_pwm_config,
> +	.enable		= ntxec_pwm_enable,
> +	.disable	= ntxec_pwm_disable,
> +	.owner		= THIS_MODULE,

Please don't align the =, just a single space before them is fine.
More important: Please implement .apply() (and .get_state()) instead of
the old API. Also please enable PWM_DEBUG which might save us a review
iteration.

> +};
> +
> +static int ntxec_pwm_probe(struct platform_device *pdev)
> +{
> +	struct ntxec *ec = dev_get_drvdata(pdev->dev.parent);
> +	struct ntxec_pwm *pwm;
> +	struct pwm_chip *chip;
> +	int res;
> +
> +	pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
> +	if (!pwm)
> +		return -ENOMEM;
> +
> +	pwm->ec = ec;
> +	pwm->dev = &pdev->dev;
> +
> +	chip = &pwm->chip;
> +	chip->dev = &pdev->dev;
> +	chip->ops = &ntxec_pwm_ops;
> +	chip->base = -1;
> +	chip->npwm = 1;
> +
> +	res = pwmchip_add(chip);
> +	if (res < 0)
> +		return res;
> +
> +	platform_set_drvdata(pdev, pwm);
> +
> +	res |= ntxec_write8(pwm->ec, NTXEC_ENABLE, 0);
> +	res |= ntxec_write8(pwm->ec, NTXEC_UNK_A, 0xff);
> +	res |= ntxec_write8(pwm->ec, NTXEC_UNK_B, 0xff);
> +
> +	return (res < 0) ? -EIO : 0;

This is broken for several reasons:

 - You're not supposed to modify the output in .probe
 - if ntxec_write8 results in an error you keep the pwm registered.
 - From the moment on pwmchip_add returns the callbacks can be called.
   The calls to ntxec_write8 probably interfere here.

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

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

  reply index

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-20 22:42 [RFC PATCH 02/10] dt-bindings: Add vendor prefix for Netronix, Inc Jonathan Neuschäfer
2020-06-20 22:42 ` [RFC PATCH 03/10] dt-bindings: mfd: Add binding for Netronix's embedded controller Jonathan Neuschäfer
2020-06-20 22:42 ` [RFC PATCH 04/10] mfd: Add base driver for Netronix " Jonathan Neuschäfer
2020-06-22 10:55   ` Lee Jones
2020-06-28  8:11     ` Jonathan Neuschäfer
2020-06-27  8:17   ` Andreas Kemnade
2020-06-28  8:29     ` Jonathan Neuschäfer
2020-06-20 22:42 ` [RFC PATCH 05/10] dt-bindings: pwm: Add bindings for PWM function in Netronix EC Jonathan Neuschäfer
2020-06-21 18:41   ` Andreas Kemnade
2020-06-20 22:42 ` [RFC PATCH 06/10] pwm: ntxec: Add driver " Jonathan Neuschäfer
2020-06-22  8:18   ` Uwe Kleine-König [this message]
2020-07-03 16:15     ` Jonathan Neuschäfer
2020-06-20 22:42 ` [RFC PATCH 07/10] dt-bindings: rtc: Add bindings for Netronix embedded controller RTC Jonathan Neuschäfer
2020-06-21  0:02   ` Alexandre Belloni
2020-06-26 21:55     ` Andreas Kemnade
2020-07-03 17:04       ` Jonathan Neuschäfer
2020-06-20 22:42 ` [RFC PATCH 08/10] rtc: New driver for RTC in Netronix embedded controller Jonathan Neuschäfer
2020-06-21  0:11   ` Alexandre Belloni
2020-07-04 19:23     ` Jonathan Neuschäfer
2020-06-20 22:42 ` [RFC PATCH 09/10] MAINTAINERS: Add entry for " Jonathan Neuschäfer
2020-06-20 22:42 ` [RFC PATCH 10/10] ARM: dts: imx50-kobo-aura: Add " Jonathan Neuschäfer
2020-06-20 22:46 ` [RFC PATCH 02/10] dt-bindings: Add vendor prefix for Netronix, Inc 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=20200622081802.pv4xmb7vn4te5r5t@taurus.defre.kleine-koenig.org \
    --to=u.kleine-koenig@pengutronix.de \
    --cc=a.zummo@towertech.it \
    --cc=alexandre.belloni@bootlin.com \
    --cc=allen.chen@ite.com.tw \
    --cc=andreas@kemnade.info \
    --cc=broonie@kernel.org \
    --cc=davem@davemloft.net \
    --cc=devicetree@vger.kernel.org \
    --cc=festevam@gmail.com \
    --cc=heiko.stuebner@theobroma-systems.com \
    --cc=heiko@sntech.de \
    --cc=j.neuschaefer@gmx.net \
    --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 \
    /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

Linux-RTC Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-rtc/0 linux-rtc/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-rtc linux-rtc/ https://lore.kernel.org/linux-rtc \
		linux-rtc@vger.kernel.org
	public-inbox-index linux-rtc

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-rtc


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git