linux-pwm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Jonathan Neuschäfer" <j.neuschaefer-hi6Y0CQ0nG0@public.gmane.org>
To: "Uwe Kleine-König"
	<u.kleine-koenig-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
Cc: "Jonathan Neuschäfer"
	<j.neuschaefer-hi6Y0CQ0nG0@public.gmane.org>,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	"Alexandre Belloni"
	<alexandre.belloni-LDxbnhwyfcJBDgjK7y7TUQ@public.gmane.org>,
	"Heiko Stuebner" <heiko-4mtYJXux2i+zQB+pC5nmwQ@public.gmane.org>,
	linux-pwm-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	"Linus Walleij"
	<linus.walleij-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>,
	"Thierry Reding"
	<thierry.reding-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
	"Fabio Estevam"
	<festevam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
	linux-rtc-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	"Mauro Carvalho Chehab"
	<mchehab+huawei-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
	"Sam Ravnborg" <sam-uyr5N9Q2VtJg9hUCZPvPmw@public.gmane.org>,
	"Andreas Kemnade"
	<andreas-cLv4Z9ELZ06ZuzBka8ofvg@public.gmane.org>,
	"NXP Linux Team" <linux-imx-3arQi8VN3Tc@public.gmane.org>,
	devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	"Stephan Gerhold"
	<stephan-3XONVrnlUWDR7s880joybQ@public.gmane.org>,
	allen <allen.chen-4K37Az1KcQB9nmWX13WWKA@public.gmane.org>,
	"Sascha Hauer" <s.hauer-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>,
	"Lubomir Rintel" <lkundrak-NGH9Lh4a5iE@public.gmane.org>,
	"Rob Herring" <robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
	"Lee Jones" <lee.jones-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
Subject: Re: [RFC PATCH 06/10] pwm: ntxec: Add driver for PWM function in Netronix EC
Date: Fri, 3 Jul 2020 18:15:56 +0200	[thread overview]
Message-ID: <20200703161556.GA2578@latitude> (raw)
In-Reply-To: <20200622081802.pv4xmb7vn4te5r5t-T6qyLwKrzP+Pq0V0m3QNwQq/OYV65a7L4Y2cMoPwMik@public.gmane.org>

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

On Mon, Jun 22, 2020 at 10:18:02AM +0200, Uwe Kleine-König wrote:
> 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

Ok, I'll fix these.

> 
> > +#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.

No particular reason; I possibly got confused by the division API. I'll
use uint32_t instead.

> > +	int res = 0;
> > +
> > +	do_div(period, 125);
> 
> Please use a define instead of plain 125.

Will do.

> > +	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.

Ok, I'll remove it.
> 
> > +	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?

Good question. I haven't measured it, and also don't have the code
running on the EC.

> 
> > +
> > +	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.

Ok

> 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.

Will do!

> 
> > +};
> > +
> > +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.

Ok, I'll rework the probe function to avoid these issues.


Thanks for the review,
Jonathan Neuschäfer

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

  parent reply	other threads:[~2020-07-03 16:15 UTC|newest]

Thread overview: 23+ 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
     [not found] ` <20200620224222.1312520-1-j.neuschaefer-hi6Y0CQ0nG0@public.gmane.org>
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
     [not found]     ` <20200620224222.1312520-4-j.neuschaefer-hi6Y0CQ0nG0@public.gmane.org>
2020-06-21 18:41       ` Andreas Kemnade
2020-08-23 22:42         ` Jonathan Neuschäfer
2020-06-20 22:42   ` [RFC PATCH 09/10] MAINTAINERS: Add entry for Netronix embedded controller Jonathan Neuschäfer
2020-06-20 22:42 ` [RFC PATCH 06/10] pwm: ntxec: Add driver for PWM function in Netronix EC Jonathan Neuschäfer
2020-06-22  8:18   ` Uwe Kleine-König
     [not found]     ` <20200622081802.pv4xmb7vn4te5r5t-T6qyLwKrzP+Pq0V0m3QNwQq/OYV65a7L4Y2cMoPwMik@public.gmane.org>
2020-07-03 16:15       ` Jonathan Neuschäfer [this message]
2020-06-20 22:42 ` [RFC PATCH 07/10] dt-bindings: rtc: Add bindings for Netronix embedded controller RTC Jonathan Neuschäfer
     [not found]   ` <20200620224222.1312520-6-j.neuschaefer-hi6Y0CQ0nG0@public.gmane.org>
2020-06-21  0:02     ` Alexandre Belloni
     [not found]       ` <20200621000220.GB131826-m++hUPXGwpdeoWH0uzbU5w@public.gmane.org>
2020-06-26 21:55         ` Andreas Kemnade
     [not found]           ` <20200626235552.7820a999-cLv4Z9ELZ06ZuzBka8ofvg@public.gmane.org>
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
     [not found]   ` <20200620224222.1312520-7-j.neuschaefer-hi6Y0CQ0nG0@public.gmane.org>
2020-06-21  0:11     ` Alexandre Belloni
     [not found]       ` <20200621001106.GC131826-m++hUPXGwpdeoWH0uzbU5w@public.gmane.org>
2020-07-04 19:23         ` 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=20200703161556.GA2578@latitude \
    --to=j.neuschaefer-hi6y0cq0ng0@public.gmane.org \
    --cc=alexandre.belloni-LDxbnhwyfcJBDgjK7y7TUQ@public.gmane.org \
    --cc=allen.chen-4K37Az1KcQB9nmWX13WWKA@public.gmane.org \
    --cc=andreas-cLv4Z9ELZ06ZuzBka8ofvg@public.gmane.org \
    --cc=devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=festevam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=heiko-4mtYJXux2i+zQB+pC5nmwQ@public.gmane.org \
    --cc=lee.jones-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org \
    --cc=linus.walleij-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org \
    --cc=linux-imx-3arQi8VN3Tc@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-pwm-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-rtc-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=lkundrak-NGH9Lh4a5iE@public.gmane.org \
    --cc=mchehab+huawei-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    --cc=robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    --cc=s.hauer-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org \
    --cc=sam-uyr5N9Q2VtJg9hUCZPvPmw@public.gmane.org \
    --cc=stephan-3XONVrnlUWDR7s880joybQ@public.gmane.org \
    --cc=thierry.reding-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=u.kleine-koenig-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org \
    /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).