Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1848622pxu; Tue, 24 Nov 2020 10:16:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJxkBPgJs3WefalnxWYNMJIXsAlHdUJAi1A+SySS+wNlE3PE5eZhKA40p7OVgCgScztQo69F X-Received: by 2002:a17:906:d72:: with SMTP id s18mr5249117ejh.110.1606241795992; Tue, 24 Nov 2020 10:16:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606241795; cv=none; d=google.com; s=arc-20160816; b=J1ZBqrGB10RsHVieoFV43entUPcIhCKh4+p763B/sNNarNjRthTRY6bchTcX7gDSJl Ae/4Mp0nPi5F6JmRFQVj9ZW0x9Hsvs0WjlpePsBbK6rJ6nD1278zQXlCs42N9+Lr3eHv KZiGjB0Si3DGLWJZhmrc0S0/287gYMRYpuJyAa89GYNm+qrKOGdaJDlli5ZSK2vIVOvT zgTP1e3t7h5+tiJiMwOMWZ24QadQ8vJI9zPFYSK0Ff7yEK5rDHAeMdqbkEwEtk7Kywtm FJkQKxQPmKAjkD91gs5FHXqoSQ82YQxcCx7Hhy/GAB/ypWI8wtNXDOCqw9KyMiKaj765 n4dA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=lTkmKbBJFp0hO5FfH+c8rOp1bE52vJZaTSe4LnGIHdM=; b=rX3wdIR/p46B9gPn7O1Z9VtnHtgLHI+rkKLmQdfOGVIaKAssGyEWvI8jET/gaJdF3X CEsjIRdGuED+yh0LHvASAZ70SVy7i/jVLvguUFliGxA8QeZugCrisPDrzOEcjjG1iHE6 HcvMmEeQvuZcM0A+UV774QM0HA1iqbS2Kt1hzUftXZbDeW1iqMw8TeabAUjk5581WHwc h7oeQnoD+6TFCBIdDMyCSkLbhfICRAWJkrjw+5u2J5+zrj6hDDS5HtVi/Aivd9kz1fdl FL5/uDAgT1dTUxff21CDRmY+VCUJPJpVU4n21S653+0m4SxIEBQePZm0tmpw99Rw/hIE 7bhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@pqgruber.com header.s=mail header.b=FnI3MsOa; 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; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=pqgruber.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r15si6926264edx.70.2020.11.24.10.16.13; Tue, 24 Nov 2020 10:16:35 -0800 (PST) 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; dkim=pass header.i=@pqgruber.com header.s=mail header.b=FnI3MsOa; 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; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=pqgruber.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404348AbgKXSKx (ORCPT + 99 others); Tue, 24 Nov 2020 13:10:53 -0500 Received: from mail.pqgruber.com ([52.59.78.55]:53788 "EHLO mail.pqgruber.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404168AbgKXSKx (ORCPT ); Tue, 24 Nov 2020 13:10:53 -0500 Received: from workstation.tuxnet (213-47-165-233.cable.dynamic.surfer.at [213.47.165.233]) by mail.pqgruber.com (Postfix) with ESMTPSA id 980CFC81EED; Tue, 24 Nov 2020 19:10:50 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pqgruber.com; s=mail; t=1606241450; bh=lTkmKbBJFp0hO5FfH+c8rOp1bE52vJZaTSe4LnGIHdM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FnI3MsOaIo+sv00WlVg7HwJhJnyJ/qXubNdea9sEonMpBB400BcW4o1/plKZvnkwJ IojhVZXsmmt1VNZVp+au2NMu0uJBp06dfF2vdi8RQOBPMEhj5U9YiHGbOEj/HXOW/2 bMpfF4Tb20zaQ1fVylgCa5LN+fX4ASmL1iafhXms= From: Clemens Gruber To: linux-pwm@vger.kernel.org Cc: Thierry Reding , u.kleine-koenig@pengutronix.de, Lee Jones , linux-kernel@vger.kernel.org, Sven Van Asbroeck , Mika Westerberg , David Jander , Clemens Gruber Subject: [PATCH v3 3/4] pwm: pca9685: Support staggered output ON times Date: Tue, 24 Nov 2020 19:10:13 +0100 Message-Id: <20201124181013.162176-3-clemens.gruber@pqgruber.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201124181013.162176-1-clemens.gruber@pqgruber.com> References: <20201124181013.162176-1-clemens.gruber@pqgruber.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The PCA9685 supports staggered LED output ON times to minimize current surges and reduce EMI. When this new option is enabled, the ON times of each channel are delayed by channel number x counter range / 16, which avoids asserting all enabled outputs at the same counter value while still maintaining the configured duty cycle of each output. Signed-off-by: Clemens Gruber --- Changes since v1: - Rebased drivers/pwm/pwm-pca9685.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/drivers/pwm/pwm-pca9685.c b/drivers/pwm/pwm-pca9685.c index 2b82b35ff0de..37c80bc8edcf 100644 --- a/drivers/pwm/pwm-pca9685.c +++ b/drivers/pwm/pwm-pca9685.c @@ -75,6 +75,7 @@ struct pca9685 { struct pwm_chip chip; struct regmap *regmap; int prescale; + bool staggered_outputs; #if IS_ENABLED(CONFIG_GPIOLIB) struct mutex lock; struct gpio_chip gpio; @@ -329,7 +330,7 @@ static int pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, { struct pca9685 *pca = to_pca(chip); unsigned long long duty, prescale; - unsigned int reg; + unsigned int on, off, reg; if (state->polarity != PWM_POLARITY_NORMAL) return -EOPNOTSUPP; @@ -375,6 +376,32 @@ static int pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, duty = (PCA9685_COUNTER_RANGE - 1) * state->duty_cycle; duty = DIV_ROUND_UP_ULL(duty, state->period); + if (pca->staggered_outputs) { + if (pwm->hwpwm < PCA9685_MAXCHAN) { + /* + * To reduce EMI, the ON times of each channel are + * spread out evenly within the counter range, while + * still maintaining the configured duty cycle + */ + on = pwm->hwpwm * PCA9685_COUNTER_RANGE / + PCA9685_MAXCHAN; + off = (on + duty) % PCA9685_COUNTER_RANGE; + regmap_write(pca->regmap, LED_N_ON_L(pwm->hwpwm), + on & 0xff); + regmap_write(pca->regmap, LED_N_ON_H(pwm->hwpwm), + (on >> 8) & 0xf); + regmap_write(pca->regmap, LED_N_OFF_L(pwm->hwpwm), + off & 0xff); + regmap_write(pca->regmap, LED_N_OFF_H(pwm->hwpwm), + (off >> 8) & 0xf); + return 0; + } + + /* No staggering possible if "all LEDs" channel is used */ + regmap_write(pca->regmap, PCA9685_ALL_LED_ON_L, 0); + regmap_write(pca->regmap, PCA9685_ALL_LED_ON_H, 0); + } + if (pwm->hwpwm >= PCA9685_MAXCHAN) reg = PCA9685_ALL_LED_OFF_L; else @@ -470,6 +497,9 @@ static int pca9685_pwm_probe(struct i2c_client *client, regmap_write(pca->regmap, PCA9685_MODE2, reg); + pca->staggered_outputs = device_property_read_bool( + &client->dev, "staggered-outputs"); + /* Disable all LED ALLCALL and SUBx addresses to avoid bus collisions */ regmap_read(pca->regmap, PCA9685_MODE1, ®); reg &= ~(MODE1_ALLCALL | MODE1_SUB1 | MODE1_SUB2 | MODE1_SUB3); @@ -478,6 +508,9 @@ static int pca9685_pwm_probe(struct i2c_client *client, /* Reset OFF registers to HW default (only full OFF bit is set) */ regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0); regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, LED_FULL); + /* Reset ON registers to HW default */ + regmap_write(pca->regmap, PCA9685_ALL_LED_ON_L, 0); + regmap_write(pca->regmap, PCA9685_ALL_LED_ON_H, 0); pca->chip.ops = &pca9685_pwm_ops; /* Add an extra channel for ALL_LED */ -- 2.29.2