Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp825348pxf; Wed, 7 Apr 2021 12:32:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx/4kWxLiYVpp6usAr9W8f1Eeq8lbCh/FO/92LtVmhzSf2LJJzWjlk3S4rJ+76fBxVWX6QA X-Received: by 2002:a05:6602:737:: with SMTP id g23mr3816267iox.130.1617823930973; Wed, 07 Apr 2021 12:32:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617823930; cv=none; d=google.com; s=arc-20160816; b=SzTG7NGnyjaAk8kCuKSPBKi0uilCuCAnop5uFLeD00ZURzsymQ+vDeDyjDxfrBaCpk Ac5nMKJyfSuHfJz9f6BcTTRg7N2tIrHB7JtS4pq5OJMZCFRLNl8un8Vdf52yH6KFn0aH BsqoRek4MEOnLmmqDNWgfhAt5/SogDQkZ83s2zHP9g7Vx70U4RwtZaOjEW/YTYDJD+uQ zf+y2es4FeSVrLfan9KmrjP3NoDMM3qqSGWibHnlnz9UpIrZzfkZ+eR9iRQYO3qwh6yV Wo4oA8DHFfXcnA3ffYzi3YYGrGIITCyX1KOtEC4jyPPNgKLv0H5KMPsFSBzGaRXwcrn9 JOQw== 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=jvL4vUHsKDszBdeB4wGMmiLXzhDHymdBMsiBcl+5FE4=; b=nZMGWj1et8anYyknAfdLWHbxbIeQRTkhxLM9V46E2Z4AjyFOD1Eva37oba1l3Ms2NA wlYOMdw6ipKWLISPaPzcsqovKE+N7jPiET8TUpa7339Xip/slQ1edBWAsvWSnqBZ6VZa cTzErej+4eWN7xFvMLhi+N2SGhg0t3rI6T13dmQ3AOwNl/hfkwoGI9p8tmFFzKRRZe6O uai475tIs1mKKk7NYjgOp1CWA0UXltKMOSk9j3U9Zm2SWaP0RQi85Y+1i3KszJOW8OJp FHnXZSG9mPFN6zNIhFJFoIWE7MO5IeUYD+mrcd/c8CiqDN1KRyTPNWbpCnV2VzuSRFpv PIYQ== 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 s190si23377681ios.22.2021.04.07.12.31.54; Wed, 07 Apr 2021 12:32:10 -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 S1345463AbhDGGMq (ORCPT + 99 others); Wed, 7 Apr 2021 02:12:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244644AbhDGGMn (ORCPT ); Wed, 7 Apr 2021 02:12:43 -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 9DDEAC06175F for ; Tue, 6 Apr 2021 23:12: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 1lU1QU-00053N-3v; Wed, 07 Apr 2021 08:12:30 +0200 Received: from ukl by ptx.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1lU1QT-0004F8-QF; Wed, 07 Apr 2021 08:12:29 +0200 Date: Wed, 7 Apr 2021 08:12: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 , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v7 7/8] pwm: pca9685: Restrict period change for enabled PWMs Message-ID: <20210407061229.lsyg7blh3ebxtvx6@pengutronix.de> References: <20210406164140.81423-1-clemens.gruber@pqgruber.com> <20210406164140.81423-7-clemens.gruber@pqgruber.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="tn42657ndmzwffdw" Content-Disposition: inline In-Reply-To: <20210406164140.81423-7-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 --tn42657ndmzwffdw Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Apr 06, 2021 at 06:41:39PM +0200, Clemens Gruber wrote: > Previously, the last used PWM channel could change the global prescale > setting, even if other channels are already in use. >=20 > Fix it by only allowing the first enabled PWM to change the global > chip-wide prescale setting. If there is more than one channel in use, > the prescale settings resulting from the chosen periods must match. >=20 > GPIOs do not count as enabled PWMs as they are not using the prescaler > and can't change it. >=20 > Signed-off-by: Clemens Gruber > --- > Changes since v6: > - Only allow the first PWM that is enabled to change the prescaler, not > the first one that uses the prescaler >=20 > drivers/pwm/pwm-pca9685.c | 66 +++++++++++++++++++++++++++++++++------ > 1 file changed, 56 insertions(+), 10 deletions(-) >=20 > diff --git a/drivers/pwm/pwm-pca9685.c b/drivers/pwm/pwm-pca9685.c > index 24221ee7a77a..cf0c98e4ef44 100644 > --- a/drivers/pwm/pwm-pca9685.c > +++ b/drivers/pwm/pwm-pca9685.c > @@ -23,11 +23,11 @@ > #include > =20 > /* > - * Because the PCA9685 has only one prescaler per chip, changing the per= iod of > - * one channel affects the period of all 16 PWM outputs! > - * However, the ratio between each configured duty cycle and the chip-wi= de > - * period remains constant, because the OFF time is set in proportion to= the > - * counter range. > + * Because the PCA9685 has only one prescaler per chip, only the first c= hannel > + * that is enabled is allowed to change the prescale register. > + * PWM channels requested afterwards must use a period that results in t= he same > + * prescale setting as the one set by the first requested channel. > + * GPIOs do not count as enabled PWMs as they are not using the prescale= r. > */ > =20 > #define PCA9685_MODE1 0x00 > @@ -78,8 +78,9 @@ > struct pca9685 { > struct pwm_chip chip; > struct regmap *regmap; > -#if IS_ENABLED(CONFIG_GPIOLIB) > struct mutex lock; > + DECLARE_BITMAP(pwms_enabled, PCA9685_MAXCHAN + 1); > +#if IS_ENABLED(CONFIG_GPIOLIB) > struct gpio_chip gpio; > DECLARE_BITMAP(pwms_inuse, PCA9685_MAXCHAN + 1); > #endif > @@ -90,6 +91,22 @@ static inline struct pca9685 *to_pca(struct pwm_chip *= chip) > return container_of(chip, struct pca9685, chip); > } > =20 > +/* This function is supposed to be called with the lock mutex held */ > +static bool pca9685_prescaler_can_change(struct pca9685 *pca, int channe= l) > +{ > + /* No PWM enabled: Change allowed */ > + if (bitmap_empty(pca->pwms_enabled, PCA9685_MAXCHAN + 1)) > + return true; > + /* More than one PWM enabled: Change not allowed */ > + if (bitmap_weight(pca->pwms_enabled, PCA9685_MAXCHAN + 1) > 1) > + return false; > + /* > + * Only one PWM enabled: Change allowed if the PWM about to > + * be changed is the one that is already enabled > + */ > + return test_bit(channel, pca->pwms_enabled); Maybe this is a bit more effective?: DECLARE_BITMAP(blablub, PCA9685_MAXCHAN + 1);=09 bitmap_zero(blablub, PCA9685_MAXCHAN + 1); bitmap_set(blablub, channel); return bitmap_subset(pca->pwms_enabled, blablub); (but that's a minor issue, the suggested algorithm is correct.) So: Acked-by: Uwe Kleine-K=F6nig (side-note: I wonder if the handling of the set-all channel is correct here. But given that it is messy anyhow, (e.g. because setting some state to this set-all channel doesn't influence pwm_get_state for the individual channels) I don't object if there is another problem in this corner case. IMHO just dropping this virtual channel would be nice.) Best regards Uwe --=20 Pengutronix e.K. | Uwe Kleine-K=F6nig | Industrial Linux Solutions | https://www.pengutronix.de/ | --tn42657ndmzwffdw Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEEfnIqFpAYrP8+dKQLwfwUeK3K7AkFAmBtTUoACgkQwfwUeK3K 7AkvNAf/UX3hYfAGdc4ZSGn8iEUYrkPpj8inn5BAMcrpoJ6rW/6mR1kS1L+YqVxI +hDPOX8DZiotLKIQEBh+zKvYkyvohTKQ2vcn40qRUDfBikSYuoeP6NMfvmFXkI6F hsSknJ5ojwHw9WX/tNVm3aCM6Th1fz0iVr7z1v+91DNHlm9UmOTM4eDYoQvchGPd dcMJEeT5lkBLZ9OPmrVh9BKbZQLepNyZJHSRjNZucoubvzT7TgNZwyyZ/45VGfJt hP6LpuontscPWq906LOKaKJJUHsqj0Y5CHAtD0aAMiV/Zj8QfHBd80eAnLSIQwla +zqivQWfITI9x/lgnl3CFXpy/6K96Q== =rzIS -----END PGP SIGNATURE----- --tn42657ndmzwffdw--