Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3568661pxf; Mon, 29 Mar 2021 06:00:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyUr2OXedvgEtB0g8fIzvIvNg4XQ8kShUx+uzK/JyaUnyMU26yV05q1E1c1I1HAjiwEieOt X-Received: by 2002:a17:906:524f:: with SMTP id y15mr28069380ejm.65.1617022849024; Mon, 29 Mar 2021 06:00:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617022849; cv=none; d=google.com; s=arc-20160816; b=IqnLJaM1nHYLXEZKBJrexIoy9PoeibAavH5Sx0DApZ5KwWzngd8aNBoEA6SxPoC98g 7+XTDEwusEIW0RuiRRW61fXLhncVu79WGTzL163oTzF5drR/Mwy4DBhBp7t1NUQ+aiFJ 4VDwacnxt2/zon3WCC6sff+Dgywb8QOGftWeEBP7nqLciCZqDnCk6nFTftxyrKCv2Pxy 2P/kCXW40EMK3ZSiMJ2rcOoQznkG7oICSg4fNJhs6ufDvUpgNwYTzdG7nhSsZ9a0d9QR 3jkBjAyuO4X4s6U5QP4/FlU41Yub9fvBWGqfiP+6WPYTg0RP6QKuqFb+a7tYGbWFy93W AnUg== 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=4ELuNUPSMc13bRxSJqgUir+ULqspUkoZCvlmwEYQQrs=; b=qpbpJi4iBT05OriKjxpAcQxwnJS4paj3LcAoStA8LQBak83pzG+CBmS36H7WH+2DQX Ajj76n+v5hPDaa+LAPxhdvaHNACynj5fAE7o5JRZUAPH6suoVKMo7QeGTrOd1rMNZP5V Xu6ROnoAxTBK0VRpQ0baj8JvARtjNa7y56Pnqs6oYYR1vL8CCXzu6oRj0HM2f/atN7wL SQsWPsSmizngosWmXjWnAJU8tc3FMBjBR6Eb2MOsMS8006uBp3WtGFfJr6mpVvdCFJZN XVW5dpFstJmlsk/mb9SKuBeYQhZbFL74TF2ETAr7xzR6Hg3OO4nf5KnYy3JNuKC4EVQa y3Qg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@pqgruber.com header.s=mail header.b=MK+x+Tm7; 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=REJECT 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 m4si13223948edc.119.2021.03.29.06.00.26; Mon, 29 Mar 2021 06:00: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; dkim=pass header.i=@pqgruber.com header.s=mail header.b=MK+x+Tm7; 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=REJECT sp=NONE dis=NONE) header.from=pqgruber.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231311AbhC2M7B (ORCPT + 99 others); Mon, 29 Mar 2021 08:59:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230493AbhC2M6f (ORCPT ); Mon, 29 Mar 2021 08:58:35 -0400 Received: from mail.pqgruber.com (mail.pqgruber.com [IPv6:2a05:d014:575:f70b:4f2c:8f1d:40c4:b13e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1280C061762; Mon, 29 Mar 2021 05:58:34 -0700 (PDT) Received: from workstation.tuxnet (213-47-165-233.cable.dynamic.surfer.at [213.47.165.233]) by mail.pqgruber.com (Postfix) with ESMTPSA id B37DAC72850; Mon, 29 Mar 2021 14:58:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pqgruber.com; s=mail; t=1617022712; bh=4ELuNUPSMc13bRxSJqgUir+ULqspUkoZCvlmwEYQQrs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MK+x+Tm7lSQNA/Ggak/MG60kWPBhovEPtkCWQq3qM4ps8Ew4IeT1gKxnRFigIqudB vIvYfY00+iUumi6/3WCML8u5uoM0EVp1W5velOm9mTDZiYl7+NHqnBhtKU2RWNSXzf p1tFuhbo1A8fK3BK4P+GXfLmqDzDwwIAV1vGi1Ys= From: Clemens Gruber To: linux-pwm@vger.kernel.org Cc: Thierry Reding , Sven Van Asbroeck , u.kleine-koenig@pengutronix.de, linux-kernel@vger.kernel.org, Clemens Gruber Subject: [PATCH v6 2/7] pwm: pca9685: Support hardware readout Date: Mon, 29 Mar 2021 14:57:02 +0200 Message-Id: <20210329125707.182732-2-clemens.gruber@pqgruber.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210329125707.182732-1-clemens.gruber@pqgruber.com> References: <20210329125707.182732-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 Implements .get_state to read-out the current hardware state. 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. 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. Signed-off-by: Clemens Gruber --- drivers/pwm/pwm-pca9685.c | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) 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; } +static void pca9685_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, + struct pwm_state *state) +{ + struct pca9685 *pca = to_pca(chip); + unsigned long long duty; + unsigned int val = 0; + + /* Calculate (chip-wide) period from prescale value */ + regmap_read(pca->regmap, PCA9685_PRESCALE, &val); + state->period = (PCA9685_COUNTER_RANGE * 1000 / PCA9685_OSC_CLOCK_MHZ) * + (val + 1); + + /* The (per-channel) polarity is fixed */ + state->polarity = PWM_POLARITY_NORMAL; + + if (pwm->hwpwm >= PCA9685_MAXCHAN) { + /* + * The "all LEDs" channel does not support HW readout + * Return 0 and disabled for backwards compatibility + */ + state->duty_cycle = 0; + state->enabled = false; + return; + } + + duty = pca9685_pwm_get_duty(pca, pwm->hwpwm); + + state->enabled = !!duty; + if (!state->enabled) { + state->duty_cycle = 0; + return; + } else if (duty == PCA9685_COUNTER_RANGE) { + state->duty_cycle = state->period; + return; + } + + duty *= state->period; + state->duty_cycle = duty / PCA9685_COUNTER_RANGE; +} + static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) { struct pca9685 *pca = to_pca(chip); @@ -355,6 +395,7 @@ static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) static const struct pwm_ops pca9685_pwm_ops = { .apply = pca9685_pwm_apply, + .get_state = pca9685_pwm_get_state, .request = pca9685_pwm_request, .free = pca9685_pwm_free, .owner = THIS_MODULE, -- 2.31.1