Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3734224pxf; Mon, 29 Mar 2021 09:56:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwonRO2CkadUSbQ/UfbkoPv4oJFBTPCvFo0BPs6ty1bk4p1Ak4WDatBUpWy6y5tJN34SAtk X-Received: by 2002:a05:6402:19a:: with SMTP id r26mr29060119edv.44.1617036997518; Mon, 29 Mar 2021 09:56:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617036997; cv=none; d=google.com; s=arc-20160816; b=CqZwpXEar8wxxv62WCASyi3k/B3tTV3XIcQrlAgL7yUr9bvwQ1h22rZbaklFSVxENY CPvEHNcZ8NxikSxt+PAcXWD8q8PIuVmdtG0Bh4MC3oMSYEE0YTD59iKxZYkbKTiK0Awr b65nKhDuSH9Hu30TsGMUnuf32zlLywtfO/csigUamEHY9qaqqvkHmIb65mbRO1lYw2eu WzS3dQxsR3vZ59Sbt/SsJAYx9Q3oUe0vHifLvrwbyNVo1hxl8ukVboSi7z5NE9w3Cs38 ocUTSt0QXFKeAXtxFPh8S8LfgvwZoEE47GG+NqNQzy2AyvbVHQj759ZtCbkS5FjAJM+w YRhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=/K8i2oFqIxyliMc5Pv5yOOZyZMjq1pqWeEA2lxH2eG0=; b=LStEwhRD7iJXlwaAJ/6pjU8jp8oZiHJxRF0PeGJRa4756WVVI8wJVJmCFtUNo3fFh7 mwU0A8Z3cMx7pCPxV0RwM3kW3XXHCVoNyQGfJ0E9cJoF59tejqXAYMT+wugIDAwOhpfX agINk/DnGaAMfFcZKnGyKXoqQw2nWZm62EBZUQkRl1KB9yh8Ljv3pLFT0MOjNurzozZT V1XHdSpI99uT61sc9+Q3lqNwfxqBRELJ8YQnKZrgeYSux3QCyN3B0zs/QT5Z8ebIR0eq FuYcqqpK2F2nE/6YW7BqPneH/Aw+l2VhFliv6aAZ1H5UbL/oiwWNOVp5erZMqTYWVP2X kXUg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h21si13311433ede.37.2021.03.29.09.56.14; Mon, 29 Mar 2021 09:56:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230271AbhC2Qy7 (ORCPT + 99 others); Mon, 29 Mar 2021 12:54:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230323AbhC2Qye (ORCPT ); Mon, 29 Mar 2021 12:54:34 -0400 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 62A21C061574 for ; Mon, 29 Mar 2021 09:54:34 -0700 (PDT) Received: from ptx.hi.pengutronix.de ([2001:67c:670:100:1d::c0]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lQv9q-0004zl-2r; Mon, 29 Mar 2021 18:54:30 +0200 Received: from ukl by ptx.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1lQv9p-0005CS-Mf; Mon, 29 Mar 2021 18:54:29 +0200 Date: Mon, 29 Mar 2021 18:54:29 +0200 From: Uwe =?utf-8?Q?Kleine-K=C3=B6nig?= To: Clemens Gruber Cc: linux-pwm@vger.kernel.org, Thierry Reding , Sven Van Asbroeck , linux-kernel@vger.kernel.org Subject: Re: [PATCH v6 2/7] pwm: pca9685: Support hardware readout Message-ID: <20210329165429.ookfliw4eq6zz2sg@pengutronix.de> References: <20210329125707.182732-1-clemens.gruber@pqgruber.com> <20210329125707.182732-2-clemens.gruber@pqgruber.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="jj5t55ipcok53hai" Content-Disposition: inline In-Reply-To: <20210329125707.182732-2-clemens.gruber@pqgruber.com> X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::c0 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 --jj5t55ipcok53hai Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Mar 29, 2021 at 02:57:02PM +0200, Clemens Gruber wrote: > Implements .get_state to read-out the current hardware state. >=20 > The hardware readout may return slightly different values than those > that were set in apply due to the limited range of possible prescale and > counter register values. >=20 > Also note that although the datasheet mentions 200 Hz as default > frequency when using the internal 25 MHz oscillator, the calculated > period from the default prescaler register setting of 30 is 5079040ns. >=20 > Signed-off-by: Clemens Gruber > --- > drivers/pwm/pwm-pca9685.c | 41 +++++++++++++++++++++++++++++++++++++++ > 1 file changed, 41 insertions(+) >=20 > diff --git a/drivers/pwm/pwm-pca9685.c b/drivers/pwm/pwm-pca9685.c > index 0ed1013737e3..fb026a25fb61 100644 > --- a/drivers/pwm/pwm-pca9685.c > +++ b/drivers/pwm/pwm-pca9685.c > @@ -333,6 +333,46 @@ static int pca9685_pwm_apply(struct pwm_chip *chip, = struct pwm_device *pwm, > return 0; > } > =20 > +static void pca9685_pwm_get_state(struct pwm_chip *chip, struct pwm_devi= ce *pwm, > + struct pwm_state *state) > +{ > + struct pca9685 *pca =3D to_pca(chip); > + unsigned long long duty; > + unsigned int val =3D 0; > + > + /* Calculate (chip-wide) period from prescale value */ > + regmap_read(pca->regmap, PCA9685_PRESCALE, &val); > + state->period =3D (PCA9685_COUNTER_RANGE * 1000 / PCA9685_OSC_CLOCK_MHZ= ) * > + (val + 1); As we have PCA9685_OSC_CLOCK_MHZ =3D 25 this is an integer calculation without loss of precision. It might be worth to point that out in a comment. (Otherwise doing the division at the end might be more sensible.) > + /* The (per-channel) polarity is fixed */ > + state->polarity =3D PWM_POLARITY_NORMAL; > + > + if (pwm->hwpwm >=3D PCA9685_MAXCHAN) { > + /* > + * The "all LEDs" channel does not support HW readout > + * Return 0 and disabled for backwards compatibility > + */ > + state->duty_cycle =3D 0; > + state->enabled =3D false; > + return; > + } > + > + duty =3D pca9685_pwm_get_duty(pca, pwm->hwpwm); > + > + state->enabled =3D !!duty; > + if (!state->enabled) { > + state->duty_cycle =3D 0; > + return; > + } else if (duty =3D=3D PCA9685_COUNTER_RANGE) { > + state->duty_cycle =3D state->period; > + return; > + } > + > + duty *=3D state->period; > + state->duty_cycle =3D duty / PCA9685_COUNTER_RANGE; =2Eapply uses ROUND_CLOSEST to calculate duty from state->duty_cycle, still using / here (instead of ROUND_CLOSEST), but again as PCA9685_OSC_CLOCK_MHZ is 25 this calculation doesn't suffer from rounding errors. So if you feed the state returned here into .apply again, there is (as I want it) no change. The only annoyance is that if PCA9685_PRESCALE holds a value smaller than 3, .apply() will fail. Not sure there is any saner way to handle this. Best regards Uwe --=20 Pengutronix e.K. | Uwe Kleine-K=F6nig | Industrial Linux Solutions | https://www.pengutronix.de/ | --jj5t55ipcok53hai Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEEfnIqFpAYrP8+dKQLwfwUeK3K7AkFAmBiBkIACgkQwfwUeK3K 7AkXKAf9Edxq8VdhqE0oDz96RLGkPW0PEAiL6u8vB1+9uTC2lxfe6gMj6tqPZamt H/Jk4XXwbitFnMLIfgZMtfA+/CMwymtjYvd6FjY5+yt1MfFkGLyZNX5G3IccWPgE d4MUwF9bG0BGgTJHEnWqJLiJ8mkDpOX6FMYSDvRtKw9rlODXnq6wlxRws/tjtjXm SkSivomIoImBw6ZE6EHiImGjX+BzSBHeyztU5PwuxDGry05ZEaR4CX9p3sZE4GOU U7U87QEUnHLdK1+vZ/1RJAsV1RTOh4v2fZtDw3WCsW8GDjyN59B9Q8X2nMQH5Z3B K+ac83vQ/Hlzgdaa5rIliDTJ1Y3b6Q== =j+4H -----END PGP SIGNATURE----- --jj5t55ipcok53hai--