Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3766170pxf; Mon, 29 Mar 2021 10:45:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy5aWHDXQRq/wtaZS7pp0ZHmOaCj9ggBWdzKII+QKFI9DkOBzl+84z270vCUv9Q9LKu7bwn X-Received: by 2002:a17:906:e84:: with SMTP id p4mr29408529ejf.248.1617039949879; Mon, 29 Mar 2021 10:45:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617039949; cv=none; d=google.com; s=arc-20160816; b=Hr9bXwrR8Owy7k3NKf8JkVtCapaS8rXluxrYA1e85s9YxHQ0fhyP63h472IuIA1hbG CUJqfsm+TilIi27Igb/5xyFMvNdiCD/L0G1UNyEoX5BwacxjDLAalGOF/JhQlgY6XzIx DjppxgLLfhlOFyUIY6JBSwHC2dViqAO5lhnj7b7UCix+uW4RsUzoJ982NoREv60Ojguz F+giCicdlBytSSzpBqPHR/JmtmutT+ejlXp2+ArxQXlciza+KPVHylNjKU2EtUnWzUUM 6lmRwBhpYh8Lz5rK1d2pM5+q/JmpSyFqmnLYlCVrb5vZznO66cWrzISXZ49UfLNsLzxz iuQA== 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=AhjCwNMGmbpGH/oSWSENItMpQbL8uxkFD7qN2Z4+E/w=; b=HOb91Xeuo48ujKm8IvAUUUbhHrJuRnkyaibhNjYAapOMD7PB/b7h71EofPhWPJWMq1 Ann5jQpNbjLfJzLUmViydZsj6Fwhup2KKj5I+x3Ure+cohFRNCnpDPo9vXm5/sfws8dK pRQxp7PblI7wLjUS7b+j5q1+aXMFEJqevajBWP3mVGddGgLNVGjtS8UGU5K+QzBnelXi 4ZeSUt2uI9unyIrsSz9EBbwQ6v0jo2GO1fzIU2lqq93713tBGIJdneo0bRB4aBMCp8Ex 9/Wd4rN9rAfPiGOMipcEtc3+w4RLBhq8zYAXdp8iT6QX5IAE8r4vwFu1aSvd9CYqR+dL Z7ng== 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 b8si12865061edz.99.2021.03.29.10.45.26; Mon, 29 Mar 2021 10:45:49 -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 S230286AbhC2Rm2 (ORCPT + 99 others); Mon, 29 Mar 2021 13:42:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229630AbhC2Rl7 (ORCPT ); Mon, 29 Mar 2021 13:41:59 -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 DFFECC061756 for ; Mon, 29 Mar 2021 10:41:58 -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 1lQvtj-00027M-K6; Mon, 29 Mar 2021 19:41:55 +0200 Received: from ukl by ptx.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1lQvtj-0008MM-9r; Mon, 29 Mar 2021 19:41:55 +0200 Date: Mon, 29 Mar 2021 19:41:54 +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: <20210329174154.kixw3f6r5r435a45@pengutronix.de> References: <20210329125707.182732-1-clemens.gruber@pqgruber.com> <20210329125707.182732-2-clemens.gruber@pqgruber.com> <20210329165429.ookfliw4eq6zz2sg@pengutronix.de> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="okhdllggjdckkruy" Content-Disposition: inline In-Reply-To: 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 --okhdllggjdckkruy Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Mar 29, 2021 at 07:11:53PM +0200, Clemens Gruber wrote: > On Mon, Mar 29, 2021 at 06:54:29PM +0200, Uwe Kleine-K=F6nig wrote: > > On Mon, Mar 29, 2021 at 02:57:02PM +0200, Clemens Gruber wrote: > > > [...] > > > + /* 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); > >=20 > > 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.) >=20 > What comment do you have in mind? > /* 1 integer multiplication (without loss of precision) at runtime */ ? Something like: /* * PCA9685_OSC_CLOCK_MHZ is 25 and so an integer divider of * 1000. So the calculation here is only a multiplication and * we're not loosing precision. */ =20 > > > + /* 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; > >=20 > > .apply 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. > >=20 > > 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. >=20 > According to the datasheet, "The hardware forces a minimum value that > can be loaded into the PRE_SCALE register at '3'", so there should never > be anything below 3 in that register. Did you verify that the register reads back a 3 if you write a lower value into the register? Maybe the most defensive way would be: + regmap_read(pca->regmap, PCA9685_PRESCALE, &val); + /* + * According to the datasheet, the hardware forces a minimum + * value that can be loaded is 3, so if we read something lower + * assume that the hardware actually implemented a 3. + */ + if (val < 3) + val =3D 3; + state->period =3D ... =09 Best regards Uwe --=20 Pengutronix e.K. | Uwe Kleine-K=F6nig | Industrial Linux Solutions | https://www.pengutronix.de/ | --okhdllggjdckkruy Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEEfnIqFpAYrP8+dKQLwfwUeK3K7AkFAmBiEV8ACgkQwfwUeK3K 7Ak9zQf/cDqjfk6kHgcX8/J2+bnqxLRhHeTg6kkh1K5QEOyoDey0/doIEKyU36Nf UkDh0YDtZTlhc/ImiGhIu70RhI+bNeacUR7wF7CymUg1WRBwBe8MbMSt7No+DLAA 7IqTGPUdBNkkUtETkhxgVEmKj1SRTiV9Fk8ahDBghuNAG9M+3+WyzB6mbhcIGEYC ZdeOWroVntj5O3PnJF5Rd1LWcZnyV6f0k3upZ9tcBfcx5A5IBjFDMziY4RRuxZIr gg+dWBCSgngFUg5aPzXI0Usx616ejcX0xLYE4KxfCTgc5K1RL35KaAaxlTmhdwYc o7mThDKsIVgKL6tRpRXd0MfmvmXBiA== =DjYB -----END PGP SIGNATURE----- --okhdllggjdckkruy--