From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF2D8C56201 for ; Tue, 24 Nov 2020 08:20:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 68F212073C for ; Tue, 24 Nov 2020 08:20:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730480AbgKXIUk (ORCPT ); Tue, 24 Nov 2020 03:20:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726155AbgKXIUk (ORCPT ); Tue, 24 Nov 2020 03:20:40 -0500 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CAAFBC0613CF for ; Tue, 24 Nov 2020 00:20:39 -0800 (PST) Received: from pty.hi.pengutronix.de ([2001:67c:670:100:1d::c5]) by metis.ext.pengutronix.de with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khTYr-0004Ny-FW; Tue, 24 Nov 2020 09:20:29 +0100 Received: from ukl by pty.hi.pengutronix.de with local (Exim 4.89) (envelope-from ) id 1khTYj-0004BH-0Y; Tue, 24 Nov 2020 09:20:21 +0100 Date: Tue, 24 Nov 2020 09:20:19 +0100 From: Uwe =?utf-8?Q?Kleine-K=C3=B6nig?= To: Jonathan =?utf-8?Q?Neusch=C3=A4fer?= , Thierry Reding Cc: linux-kernel@vger.kernel.org, Alexandre Belloni , Heiko Stuebner , linux-pwm@vger.kernel.org, Linus Walleij , Fabio Estevam , linux-rtc@vger.kernel.org, Arnd Bergmann , Mauro Carvalho Chehab , Sam Ravnborg , Daniel Palmer , Andy Shevchenko , Andreas Kemnade , NXP Linux Team , devicetree@vger.kernel.org, Stephan Gerhold , allen , Sascha Hauer , Lubomir Rintel , Rob Herring , Lee Jones , linux-arm-kernel@lists.infradead.org, Alessandro Zummo , Mark Brown , Pengutronix Kernel Team , Heiko Stuebner , Josua Mayer , Shawn Guo , "David S. Miller" Subject: Re: [PATCH v4 4/7] pwm: ntxec: Add driver for PWM function in Netronix EC Message-ID: <20201124082019.vpkr3xnp55arjpnp@pengutronix.de> References: <20201122222739.1455132-1-j.neuschaefer@gmx.net> <20201122222739.1455132-5-j.neuschaefer@gmx.net> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="n7psjkl5oqhse6nm" Content-Disposition: inline In-Reply-To: <20201122222739.1455132-5-j.neuschaefer@gmx.net> X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::c5 X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --n7psjkl5oqhse6nm Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hello, On Sun, Nov 22, 2020 at 11:27:36PM +0100, Jonathan Neusch=E4fer wrote: > The Netronix EC provides a PWM output which is used for the backlight > on some ebook readers. This patches adds a driver for the PWM output. >=20 > The .get_state callback is not implemented, because the PWM state can't > be read back from the hardware. >=20 > Signed-off-by: Jonathan Neusch=E4fer > --- >=20 > v4: > - Document hardware/driver limitations > - Only accept normal polarity > - Fix a typo ("zone" -> "zero") > - change MAX_PERIOD_NS to 0xffff * 125 > - Clamp period to the maximum rather than returning an error > - Rename private struct pointer to priv > - Rearrage control flow in _probe to save a few lines and a temporary var= iable > - Add missing MODULE_ALIAS line > - Spell out ODM >=20 > v3: > - https://lore.kernel.org/lkml/20200924192455.2484005-5-j.neuschaefer@gmx= =2Enet/ > - Relicense as GPLv2 or later > - Add email address to copyright line > - Remove OF compatible string and don't include linux/of_device.h > - Fix bogus ?: in return line > - Don't use a comma after sentinels > - Avoid ret |=3D ... pattern > - Move 8-bit register conversion to ntxec.h >=20 > v2: > - https://lore.kernel.org/lkml/20200905133230.1014581-6-j.neuschaefer@gmx= =2Enet/ > - Various grammar and style improvements, as suggested by Uwe Kleine-K=F6= nig, > Lee Jones, and Alexandre Belloni > - Switch to regmap > - Prefix registers with NTXEC_REG_ > - Add help text to the Kconfig option > - Use the .apply callback instead of the old API > - Add a #define for the time base (125ns) > - Don't change device state in .probe; this avoids multiple problems > - Rework division and overflow check logic to perform divisions in 32 bits > - Avoid setting duty cycle to zero, to work around a hardware quirk > --- > drivers/pwm/Kconfig | 8 ++ > drivers/pwm/Makefile | 1 + > drivers/pwm/pwm-ntxec.c | 166 ++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 175 insertions(+) > create mode 100644 drivers/pwm/pwm-ntxec.c >=20 > diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig > index 63be5362fd3a5..815f329ed5b46 100644 > --- a/drivers/pwm/Kconfig > +++ b/drivers/pwm/Kconfig > @@ -350,6 +350,14 @@ config PWM_MXS > To compile this driver as a module, choose M here: the module > will be called pwm-mxs. >=20 > +config PWM_NTXEC > + tristate "Netronix embedded controller PWM support" > + depends on MFD_NTXEC > + help > + Say yes here if you want to support the PWM output of the embedded > + controller found in certain e-book readers designed by the original > + design manufacturer Netronix. > + > config PWM_OMAP_DMTIMER > tristate "OMAP Dual-Mode Timer PWM support" > depends on OF > diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile > index cbdcd55d69eef..1deb29e6ae8e5 100644 > --- a/drivers/pwm/Makefile > +++ b/drivers/pwm/Makefile > @@ -32,6 +32,7 @@ obj-$(CONFIG_PWM_MESON) +=3D pwm-meson.o > obj-$(CONFIG_PWM_MEDIATEK) +=3D pwm-mediatek.o > obj-$(CONFIG_PWM_MTK_DISP) +=3D pwm-mtk-disp.o > obj-$(CONFIG_PWM_MXS) +=3D pwm-mxs.o > +obj-$(CONFIG_PWM_NTXEC) +=3D pwm-ntxec.o > obj-$(CONFIG_PWM_OMAP_DMTIMER) +=3D pwm-omap-dmtimer.o > obj-$(CONFIG_PWM_PCA9685) +=3D pwm-pca9685.o > obj-$(CONFIG_PWM_PXA) +=3D pwm-pxa.o > diff --git a/drivers/pwm/pwm-ntxec.c b/drivers/pwm/pwm-ntxec.c > new file mode 100644 > index 0000000000000..4f4f736d71aba > --- /dev/null > +++ b/drivers/pwm/pwm-ntxec.c > @@ -0,0 +1,166 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * The Netronix embedded controller is a microcontroller found in some > + * e-book readers designed by the original design manufacturer Netronix,= Inc. > + * It contains RTC, battery monitoring, system power management, and PWM > + * functionality. > + * > + * This driver implements PWM output. > + * > + * Copyright 2020 Jonathan Neusch=E4fer > + * > + * Limitations: > + * - The get_state callback is not implemented, because the current stat= e of > + * the PWM output can't be read back from the hardware. > + * - The hardware can only generate normal polarity output. > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > + > +struct ntxec_pwm { > + struct device *dev; > + struct ntxec *ec; > + struct pwm_chip chip; > +}; > + > +static struct ntxec_pwm *pwmchip_to_priv(struct pwm_chip *chip) > +{ > + return container_of(chip, struct ntxec_pwm, chip); > +} > + > +#define NTXEC_REG_AUTO_OFF_HI 0xa1 > +#define NTXEC_REG_AUTO_OFF_LO 0xa2 > +#define NTXEC_REG_ENABLE 0xa3 > +#define NTXEC_REG_PERIOD_LOW 0xa4 > +#define NTXEC_REG_PERIOD_HIGH 0xa5 > +#define NTXEC_REG_DUTY_LOW 0xa6 > +#define NTXEC_REG_DUTY_HIGH 0xa7 > + > +/* > + * The time base used in the EC is 8MHz, or 125ns. Period and duty cycle= are > + * measured in this unit. > + */ > +#define TIME_BASE_NS 125 > + > +/* > + * The maximum input value (in nanoseconds) is determined by the time ba= se and > + * the range of the hardware registers that hold the converted value. > + * It fits into 32 bits, so we can do our calculations in 32 bits as wel= l. > + */ > +#define MAX_PERIOD_NS (TIME_BASE_NS * 0xffff) > + > +static int ntxec_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm= _dev, > + const struct pwm_state *state) > +{ > + struct ntxec_pwm *priv =3D pwmchip_to_priv(pwm_dev->chip); > + unsigned int duty =3D state->duty_cycle; > + unsigned int period =3D state->period; state->duty_cycle and state->period are u64, so you're losing information here. Consider state->duty_cycle =3D 0x100000001 and state->period =3D 0x200000001. > + int res =3D 0; > + > + if (state->polarity !=3D PWM_POLARITY_NORMAL) > + return -EINVAL; > + > + if (period > MAX_PERIOD_NS) { > + period =3D MAX_PERIOD_NS; > + > + if (duty > period) > + duty =3D period; > + } > + > + period /=3D TIME_BASE_NS; > + duty /=3D TIME_BASE_NS; > + > + res =3D regmap_write(priv->ec->regmap, NTXEC_REG_PERIOD_HIGH, ntxec_reg= 8(period >> 8)); > + if (res) > + return res; I wonder if you can add some logic to the regmap in the mfd driver such that ntxec_reg8 isn't necessary for all users. > + res =3D regmap_write(priv->ec->regmap, NTXEC_REG_PERIOD_LOW, ntxec_reg8= (period)); > + if (res) > + return res; > + > + res =3D regmap_write(priv->ec->regmap, NTXEC_REG_DUTY_HIGH, ntxec_reg8(= duty >> 8)); > + if (res) > + return res; > + > + res =3D regmap_write(priv->ec->regmap, NTXEC_REG_DUTY_LOW, ntxec_reg8(d= uty)); > + if (res) > + return res; I think I already asked, but I don't remember the reply: What happens to the output between these writes? A comment here about this would be suitable. > + > + /* > + * 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=3D0 when the duty cycle is zero. > + */ > + if (state->enabled && duty !=3D 0) { > + res =3D regmap_write(priv->ec->regmap, NTXEC_REG_ENABLE, ntxec_reg8(1)= ); > + if (res) > + return res; > + > + /* Disable the auto-off timer */ > + res =3D regmap_write(priv->ec->regmap, NTXEC_REG_AUTO_OFF_HI, ntxec_re= g8(0xff)); > + if (res) > + return res; > + > + return regmap_write(priv->ec->regmap, NTXEC_REG_AUTO_OFF_LO, ntxec_reg= 8(0xff)); > + } else { > + return regmap_write(priv->ec->regmap, NTXEC_REG_ENABLE, ntxec_reg8(0)); > + } > +} > + > +static struct pwm_ops ntxec_pwm_ops =3D { This can be const. > + .apply =3D ntxec_pwm_apply, /* * The current state cannot be read out, so there is no .get_state * callback. */ Hmm, at least you could provice a .get_state() callback that reports the setting that was actually implemented for in the last call to .apply()? @Thierry: Do you have concerns here? Actually it would be more effective to have a callback (like .apply()) that modfies its pwm_state accordingly. (Some drivers did that in the past, but I changed that to get an uniform behaviour in 71523d1812aca61e32e742e87ec064e3d8c615e1.) The downside is that people have to understand that concept to properly use it. I'm torn about the right approach. > + .owner =3D THIS_MODULE, > +}; > + > +static int ntxec_pwm_probe(struct platform_device *pdev) > +{ > + struct ntxec *ec =3D dev_get_drvdata(pdev->dev.parent); > + struct ntxec_pwm *priv; > + struct pwm_chip *chip; > + > + priv =3D devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); > + if (!priv) > + return -ENOMEM; > + > + priv->ec =3D ec; > + priv->dev =3D &pdev->dev; > + > + platform_set_drvdata(pdev, priv); > + > + chip =3D &priv->chip; > + chip->dev =3D &pdev->dev; > + chip->ops =3D &ntxec_pwm_ops; > + chip->base =3D -1; > + chip->npwm =3D 1; > + > + return pwmchip_add(chip); > +} > + > +static int ntxec_pwm_remove(struct platform_device *pdev) > +{ > + struct ntxec_pwm *priv =3D platform_get_drvdata(pdev); > + struct pwm_chip *chip =3D &priv->chip; > + > + return pwmchip_remove(chip); > +} > + > +static struct platform_driver ntxec_pwm_driver =3D { > + .driver =3D { > + .name =3D "ntxec-pwm", > + }, > + .probe =3D ntxec_pwm_probe, > + .remove =3D ntxec_pwm_remove, > +}; > +module_platform_driver(ntxec_pwm_driver); > + > +MODULE_AUTHOR("Jonathan Neusch=E4fer "); > +MODULE_DESCRIPTION("PWM driver for Netronix EC"); > +MODULE_LICENSE("GPL"); > +MODULE_ALIAS("platform:ntxec-pwm"); --=20 Pengutronix e.K. | Uwe Kleine-K=F6nig | Industrial Linux Solutions | https://www.pengutronix.de/ | --n7psjkl5oqhse6nm Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEEfnIqFpAYrP8+dKQLwfwUeK3K7AkFAl+8wkAACgkQwfwUeK3K 7Ak9bgf+NapmOS9Fdc80KOeLtGA8StDYY66jDjFZZOGLqfzX2tmGPKOeUbbJkwtu qMFOWSBJsJUz2WXM2EOO/vN5O/aXR3FDg8W4wHBLyC76yy0rzCVfkWy4KYvrps72 p/xjdZQTGZrnUKFdWfz1WGM+dja/sB5tXfIZnYJ2iJGoJ8JkOFXw/8Ug7156STd+ Nvvg4EF36jSwRi4XyIhiFxmFAdebQsMtxS4R3C9vE64ZtEibTNMCmgNgpm4lNYbm cLMf7JgCt6DAviaQl+seBPYtigRAr3z5n8tPPSRyMvPS0PNs2IQEhs18UX9g/O2E WQT2wSIwGHALFYjble0FTmZGIUdM0A== =8Z7I -----END PGP SIGNATURE----- --n7psjkl5oqhse6nm-- From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 46D50C56201 for ; Tue, 24 Nov 2020 08:21:23 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A8FE42073C for ; Tue, 24 Nov 2020 08:21:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="Tkrp/Re3" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A8FE42073C Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Type:Cc: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: In-Reply-To:MIME-Version:References:Message-ID:Subject:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=g4QbAJbgZh+V+Fo4l5LiDLwN7x+Y3st6mrWLzxwV8L4=; b=Tkrp/Re3059KI2V/5yncPFVfx t1Ld+7j52GKhzfCKd8fOJca4G1QZ/WgPuYQqP8w2ExuImvfPZUDGyLo8GQ5Ji2cvQZZcKK1mmV0zT fB2dI06VfclAcwiTvjAjfqsHzaYmwsT+SKXQds2QgtN6vYhch2pGmI/J13sZeY0n1qG/7cgzubj8S C0Lia/Bzn06V0sUAoITwGMT6Th42aIQVk/4INmw6MHV6fgP73AIchWAKwTkieMLDIEkP42N4ck+79 YC5GYOAosGpHlkoZWWlCaho+PIOON12Y1K1rxpO7MSwUFx/LeoOI7FQJ9GNCKRznHsq8M58PK2C7q u33YVF+YA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1khTZG-00023O-FW; Tue, 24 Nov 2020 08:20:54 +0000 Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1khTZD-00022r-UO for linux-arm-kernel@lists.infradead.org; Tue, 24 Nov 2020 08:20:53 +0000 Received: from pty.hi.pengutronix.de ([2001:67c:670:100:1d::c5]) by metis.ext.pengutronix.de with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khTYr-0004Ny-FW; Tue, 24 Nov 2020 09:20:29 +0100 Received: from ukl by pty.hi.pengutronix.de with local (Exim 4.89) (envelope-from ) id 1khTYj-0004BH-0Y; Tue, 24 Nov 2020 09:20:21 +0100 Date: Tue, 24 Nov 2020 09:20:19 +0100 From: Uwe =?utf-8?Q?Kleine-K=C3=B6nig?= To: Jonathan =?utf-8?Q?Neusch=C3=A4fer?= , Thierry Reding Subject: Re: [PATCH v4 4/7] pwm: ntxec: Add driver for PWM function in Netronix EC Message-ID: <20201124082019.vpkr3xnp55arjpnp@pengutronix.de> References: <20201122222739.1455132-1-j.neuschaefer@gmx.net> <20201122222739.1455132-5-j.neuschaefer@gmx.net> MIME-Version: 1.0 In-Reply-To: <20201122222739.1455132-5-j.neuschaefer@gmx.net> X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::c5 X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-arm-kernel@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201124_032052_147545_E427FB7A X-CRM114-Status: GOOD ( 49.72 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alexandre Belloni , Heiko Stuebner , devicetree@vger.kernel.org, Linus Walleij , Sam Ravnborg , linux-rtc@vger.kernel.org, Arnd Bergmann , Mauro Carvalho Chehab , Lee Jones , Daniel Palmer , Andy Shevchenko , Andreas Kemnade , NXP Linux Team , linux-pwm@vger.kernel.org, Stephan Gerhold , allen , Sascha Hauer , Lubomir Rintel , Rob Herring , Fabio Estevam , linux-arm-kernel@lists.infradead.org, Alessandro Zummo , linux-kernel@vger.kernel.org, Mark Brown , Pengutronix Kernel Team , Heiko Stuebner , Josua Mayer , Shawn Guo , "David S. Miller" Content-Type: multipart/mixed; boundary="===============8917569164060128633==" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org --===============8917569164060128633== Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="n7psjkl5oqhse6nm" Content-Disposition: inline --n7psjkl5oqhse6nm Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hello, On Sun, Nov 22, 2020 at 11:27:36PM +0100, Jonathan Neusch=E4fer wrote: > The Netronix EC provides a PWM output which is used for the backlight > on some ebook readers. This patches adds a driver for the PWM output. >=20 > The .get_state callback is not implemented, because the PWM state can't > be read back from the hardware. >=20 > Signed-off-by: Jonathan Neusch=E4fer > --- >=20 > v4: > - Document hardware/driver limitations > - Only accept normal polarity > - Fix a typo ("zone" -> "zero") > - change MAX_PERIOD_NS to 0xffff * 125 > - Clamp period to the maximum rather than returning an error > - Rename private struct pointer to priv > - Rearrage control flow in _probe to save a few lines and a temporary var= iable > - Add missing MODULE_ALIAS line > - Spell out ODM >=20 > v3: > - https://lore.kernel.org/lkml/20200924192455.2484005-5-j.neuschaefer@gmx= =2Enet/ > - Relicense as GPLv2 or later > - Add email address to copyright line > - Remove OF compatible string and don't include linux/of_device.h > - Fix bogus ?: in return line > - Don't use a comma after sentinels > - Avoid ret |=3D ... pattern > - Move 8-bit register conversion to ntxec.h >=20 > v2: > - https://lore.kernel.org/lkml/20200905133230.1014581-6-j.neuschaefer@gmx= =2Enet/ > - Various grammar and style improvements, as suggested by Uwe Kleine-K=F6= nig, > Lee Jones, and Alexandre Belloni > - Switch to regmap > - Prefix registers with NTXEC_REG_ > - Add help text to the Kconfig option > - Use the .apply callback instead of the old API > - Add a #define for the time base (125ns) > - Don't change device state in .probe; this avoids multiple problems > - Rework division and overflow check logic to perform divisions in 32 bits > - Avoid setting duty cycle to zero, to work around a hardware quirk > --- > drivers/pwm/Kconfig | 8 ++ > drivers/pwm/Makefile | 1 + > drivers/pwm/pwm-ntxec.c | 166 ++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 175 insertions(+) > create mode 100644 drivers/pwm/pwm-ntxec.c >=20 > diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig > index 63be5362fd3a5..815f329ed5b46 100644 > --- a/drivers/pwm/Kconfig > +++ b/drivers/pwm/Kconfig > @@ -350,6 +350,14 @@ config PWM_MXS > To compile this driver as a module, choose M here: the module > will be called pwm-mxs. >=20 > +config PWM_NTXEC > + tristate "Netronix embedded controller PWM support" > + depends on MFD_NTXEC > + help > + Say yes here if you want to support the PWM output of the embedded > + controller found in certain e-book readers designed by the original > + design manufacturer Netronix. > + > config PWM_OMAP_DMTIMER > tristate "OMAP Dual-Mode Timer PWM support" > depends on OF > diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile > index cbdcd55d69eef..1deb29e6ae8e5 100644 > --- a/drivers/pwm/Makefile > +++ b/drivers/pwm/Makefile > @@ -32,6 +32,7 @@ obj-$(CONFIG_PWM_MESON) +=3D pwm-meson.o > obj-$(CONFIG_PWM_MEDIATEK) +=3D pwm-mediatek.o > obj-$(CONFIG_PWM_MTK_DISP) +=3D pwm-mtk-disp.o > obj-$(CONFIG_PWM_MXS) +=3D pwm-mxs.o > +obj-$(CONFIG_PWM_NTXEC) +=3D pwm-ntxec.o > obj-$(CONFIG_PWM_OMAP_DMTIMER) +=3D pwm-omap-dmtimer.o > obj-$(CONFIG_PWM_PCA9685) +=3D pwm-pca9685.o > obj-$(CONFIG_PWM_PXA) +=3D pwm-pxa.o > diff --git a/drivers/pwm/pwm-ntxec.c b/drivers/pwm/pwm-ntxec.c > new file mode 100644 > index 0000000000000..4f4f736d71aba > --- /dev/null > +++ b/drivers/pwm/pwm-ntxec.c > @@ -0,0 +1,166 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * The Netronix embedded controller is a microcontroller found in some > + * e-book readers designed by the original design manufacturer Netronix,= Inc. > + * It contains RTC, battery monitoring, system power management, and PWM > + * functionality. > + * > + * This driver implements PWM output. > + * > + * Copyright 2020 Jonathan Neusch=E4fer > + * > + * Limitations: > + * - The get_state callback is not implemented, because the current stat= e of > + * the PWM output can't be read back from the hardware. > + * - The hardware can only generate normal polarity output. > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > + > +struct ntxec_pwm { > + struct device *dev; > + struct ntxec *ec; > + struct pwm_chip chip; > +}; > + > +static struct ntxec_pwm *pwmchip_to_priv(struct pwm_chip *chip) > +{ > + return container_of(chip, struct ntxec_pwm, chip); > +} > + > +#define NTXEC_REG_AUTO_OFF_HI 0xa1 > +#define NTXEC_REG_AUTO_OFF_LO 0xa2 > +#define NTXEC_REG_ENABLE 0xa3 > +#define NTXEC_REG_PERIOD_LOW 0xa4 > +#define NTXEC_REG_PERIOD_HIGH 0xa5 > +#define NTXEC_REG_DUTY_LOW 0xa6 > +#define NTXEC_REG_DUTY_HIGH 0xa7 > + > +/* > + * The time base used in the EC is 8MHz, or 125ns. Period and duty cycle= are > + * measured in this unit. > + */ > +#define TIME_BASE_NS 125 > + > +/* > + * The maximum input value (in nanoseconds) is determined by the time ba= se and > + * the range of the hardware registers that hold the converted value. > + * It fits into 32 bits, so we can do our calculations in 32 bits as wel= l. > + */ > +#define MAX_PERIOD_NS (TIME_BASE_NS * 0xffff) > + > +static int ntxec_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm= _dev, > + const struct pwm_state *state) > +{ > + struct ntxec_pwm *priv =3D pwmchip_to_priv(pwm_dev->chip); > + unsigned int duty =3D state->duty_cycle; > + unsigned int period =3D state->period; state->duty_cycle and state->period are u64, so you're losing information here. Consider state->duty_cycle =3D 0x100000001 and state->period =3D 0x200000001. > + int res =3D 0; > + > + if (state->polarity !=3D PWM_POLARITY_NORMAL) > + return -EINVAL; > + > + if (period > MAX_PERIOD_NS) { > + period =3D MAX_PERIOD_NS; > + > + if (duty > period) > + duty =3D period; > + } > + > + period /=3D TIME_BASE_NS; > + duty /=3D TIME_BASE_NS; > + > + res =3D regmap_write(priv->ec->regmap, NTXEC_REG_PERIOD_HIGH, ntxec_reg= 8(period >> 8)); > + if (res) > + return res; I wonder if you can add some logic to the regmap in the mfd driver such that ntxec_reg8 isn't necessary for all users. > + res =3D regmap_write(priv->ec->regmap, NTXEC_REG_PERIOD_LOW, ntxec_reg8= (period)); > + if (res) > + return res; > + > + res =3D regmap_write(priv->ec->regmap, NTXEC_REG_DUTY_HIGH, ntxec_reg8(= duty >> 8)); > + if (res) > + return res; > + > + res =3D regmap_write(priv->ec->regmap, NTXEC_REG_DUTY_LOW, ntxec_reg8(d= uty)); > + if (res) > + return res; I think I already asked, but I don't remember the reply: What happens to the output between these writes? A comment here about this would be suitable. > + > + /* > + * 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=3D0 when the duty cycle is zero. > + */ > + if (state->enabled && duty !=3D 0) { > + res =3D regmap_write(priv->ec->regmap, NTXEC_REG_ENABLE, ntxec_reg8(1)= ); > + if (res) > + return res; > + > + /* Disable the auto-off timer */ > + res =3D regmap_write(priv->ec->regmap, NTXEC_REG_AUTO_OFF_HI, ntxec_re= g8(0xff)); > + if (res) > + return res; > + > + return regmap_write(priv->ec->regmap, NTXEC_REG_AUTO_OFF_LO, ntxec_reg= 8(0xff)); > + } else { > + return regmap_write(priv->ec->regmap, NTXEC_REG_ENABLE, ntxec_reg8(0)); > + } > +} > + > +static struct pwm_ops ntxec_pwm_ops =3D { This can be const. > + .apply =3D ntxec_pwm_apply, /* * The current state cannot be read out, so there is no .get_state * callback. */ Hmm, at least you could provice a .get_state() callback that reports the setting that was actually implemented for in the last call to .apply()? @Thierry: Do you have concerns here? Actually it would be more effective to have a callback (like .apply()) that modfies its pwm_state accordingly. (Some drivers did that in the past, but I changed that to get an uniform behaviour in 71523d1812aca61e32e742e87ec064e3d8c615e1.) The downside is that people have to understand that concept to properly use it. I'm torn about the right approach. > + .owner =3D THIS_MODULE, > +}; > + > +static int ntxec_pwm_probe(struct platform_device *pdev) > +{ > + struct ntxec *ec =3D dev_get_drvdata(pdev->dev.parent); > + struct ntxec_pwm *priv; > + struct pwm_chip *chip; > + > + priv =3D devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); > + if (!priv) > + return -ENOMEM; > + > + priv->ec =3D ec; > + priv->dev =3D &pdev->dev; > + > + platform_set_drvdata(pdev, priv); > + > + chip =3D &priv->chip; > + chip->dev =3D &pdev->dev; > + chip->ops =3D &ntxec_pwm_ops; > + chip->base =3D -1; > + chip->npwm =3D 1; > + > + return pwmchip_add(chip); > +} > + > +static int ntxec_pwm_remove(struct platform_device *pdev) > +{ > + struct ntxec_pwm *priv =3D platform_get_drvdata(pdev); > + struct pwm_chip *chip =3D &priv->chip; > + > + return pwmchip_remove(chip); > +} > + > +static struct platform_driver ntxec_pwm_driver =3D { > + .driver =3D { > + .name =3D "ntxec-pwm", > + }, > + .probe =3D ntxec_pwm_probe, > + .remove =3D ntxec_pwm_remove, > +}; > +module_platform_driver(ntxec_pwm_driver); > + > +MODULE_AUTHOR("Jonathan Neusch=E4fer "); > +MODULE_DESCRIPTION("PWM driver for Netronix EC"); > +MODULE_LICENSE("GPL"); > +MODULE_ALIAS("platform:ntxec-pwm"); --=20 Pengutronix e.K. | Uwe Kleine-K=F6nig | Industrial Linux Solutions | https://www.pengutronix.de/ | --n7psjkl5oqhse6nm Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEEfnIqFpAYrP8+dKQLwfwUeK3K7AkFAl+8wkAACgkQwfwUeK3K 7Ak9bgf+NapmOS9Fdc80KOeLtGA8StDYY66jDjFZZOGLqfzX2tmGPKOeUbbJkwtu qMFOWSBJsJUz2WXM2EOO/vN5O/aXR3FDg8W4wHBLyC76yy0rzCVfkWy4KYvrps72 p/xjdZQTGZrnUKFdWfz1WGM+dja/sB5tXfIZnYJ2iJGoJ8JkOFXw/8Ug7156STd+ Nvvg4EF36jSwRi4XyIhiFxmFAdebQsMtxS4R3C9vE64ZtEibTNMCmgNgpm4lNYbm cLMf7JgCt6DAviaQl+seBPYtigRAr3z5n8tPPSRyMvPS0PNs2IQEhs18UX9g/O2E WQT2wSIwGHALFYjble0FTmZGIUdM0A== =8Z7I -----END PGP SIGNATURE----- --n7psjkl5oqhse6nm-- --===============8917569164060128633== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel --===============8917569164060128633==--