Received: by 2002:a05:7412:8d09:b0:fa:4c10:6cad with SMTP id bj9csp230287rdb; Mon, 15 Jan 2024 20:11:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IEVJRIIohlzgQPXuwDGq1gwAGWhY5p103l15phO6dPToKyBrH6UNVjhGoFoCHNeHxUjIhwy X-Received: by 2002:a17:90a:9bca:b0:28d:bcb9:466f with SMTP id b10-20020a17090a9bca00b0028dbcb9466fmr2865202pjw.5.1705378308761; Mon, 15 Jan 2024 20:11:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705378308; cv=none; d=google.com; s=arc-20160816; b=CfddGvqv3dT9uRoV7bYJbyrS0FyVFFN1pNC/FVERgJMlWpE+semUalf/L7T111sVpx XDo5k+YacUCzjZlWF/q0QduHG4I1pdJgxmNwgLzMBmF+YtRtV/0lUCVG75G+9+95z5Z4 snI2Vr0w0HGaxTqkEWuFuMRvn6lYPtyOwpPmrLXGQbUUGAjdQ5VpmciB7dJ6k3nG1a1Z XKxr+m9evRj2ONghiCgnnUKD74df4J3QGC6J14ENuTipx2QZxgEzvPeyMtQJOhdo11RE f+a35SOgyY12YtBE1yVr+ZHV02xJebX8vjtH3JfMkUIkNctxYX7J3RfMpBHyHq1HNzM2 d9Bg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=cvJrbymRDVqsWiIagFJrDLqKjAGvymGFfMj4nY9d7Jo=; fh=UkKqDtQf/nyeWncZ0OnJZ79jYcbt/5EXA6rFMCrt7Kg=; b=takj5Ma/oFcee4/qdKzdUhhY4WCDD0BkRIVIAHKtviI65baM5xJIMB56UEJb7pq7zD Acl5KWwNEs52rfE1tjQQUFOEtI02D+qJI5TMHzsHcglpbTdbyEUNz3H6QE1irRFflwzu mhOOGup2DSusAU0gtvaAgT4bWkEi8fv5a9asYYuzQIfkl3GNJhkYYE2VEuxPyyjNz4dr gq8IheAMhFJasDIoP8rMzzJ+z+j5covsWwrLbAdGujAKAqtSgU0OkkgbL8vTzDHrBkcm kCf0oo4Lqz75LZ6zEAOm6Rbm14NB14jezSGCAB5UV0oytIaSGs0PIk4sErOCtu9ZpyAv PdcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=JvpOM9LB; spf=pass (google.com: domain of linux-kernel+bounces-26950-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-26950-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id bk21-20020a17090b081500b0028d876fabbcsi10049340pjb.111.2024.01.15.20.11.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jan 2024 20:11:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-26950-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=JvpOM9LB; spf=pass (google.com: domain of linux-kernel+bounces-26950-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-26950-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 6DA572849BB for ; Tue, 16 Jan 2024 04:11:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7062E10976; Tue, 16 Jan 2024 04:11:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="JvpOM9LB" Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6AE206FA8 for ; Tue, 16 Jan 2024 04:11:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-6da4a923b1bso4676888b3a.2 for ; Mon, 15 Jan 2024 20:11:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1705378277; x=1705983077; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cvJrbymRDVqsWiIagFJrDLqKjAGvymGFfMj4nY9d7Jo=; b=JvpOM9LBmjJH09YokSHo3Ysr1GZHtTLYi5JRk7K8kDxp1bSY3ctof0fRjm64sM/XJa 3G2ksoB2uIxOPfxNHoM6pN6WJofXTeXIk35QMlgQmzW4uWBHqzyP8tSgqWwT1259cMLx R1EUKs8mucFdk9Nm034/fwDL9edifq93Yn0IjQ5E8ewZIxfEKuaKnThjXx5UPH4sjxfL mkAEEOVAR6QhJm4X3jBz47BL2vVzHxRmdtptt38G7+9rllBKStO4ey0zpUB1Rn6oS8q0 ergcwWjkdYRqFV/C+LOYK2ziDqiLeDT6gjbdhce+jjxf7MNmcQNyGdWZDI/iBanUl9E0 er3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705378277; x=1705983077; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cvJrbymRDVqsWiIagFJrDLqKjAGvymGFfMj4nY9d7Jo=; b=fdI7Sbp/NFEP7Li0SqolrJYLisWJH0InWRGka1rTBOHsB33/5QcTx0kkDp/6EgUfnw dOPRBaoKCOqQdQtCQY3zJSb4ywlGK7OIv18wmM14jkW31o+NjeoTKo7iLKlpRN/n3SiJ 5myVViE5UJDFFfqZRWyX/097apvgHOeExq8iJRynM3z5JyYbIAJZ0DIZLqaMX9HehO4O qcKZFaT9gbqFHYUUWtNj7/sSrRkhzGtpCuvCauNYkepIuk0d3BUkozQNI5ZetpRKQPdl cgNj8VogLCezSOrpWcm6wOnkG4yLE93aDidJ7xbgsEp+9/Hv5Qog3xLMKYhj85qdBJwD 1DEQ== X-Gm-Message-State: AOJu0Yyi3XiMixfF6IrTYAfJrX8W5m54hb6PYaR+t3h56KVJ9u3ECuFo c9oehSjm5UEEHJ8CLM0fsJTKkwfjYtTt4g== X-Received: by 2002:a05:6a00:2a04:b0:6da:b010:e863 with SMTP id ce4-20020a056a002a0400b006dab010e863mr3534499pfb.55.1705378276814; Mon, 15 Jan 2024 20:11:16 -0800 (PST) Received: from hsinchu15.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id e3-20020a056a0000c300b006da14f68ac1sm8348585pfj.198.2024.01.15.20.11.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jan 2024 20:11:16 -0800 (PST) From: Nylon Chen To: paul.walmsley@sifive.com, palmer@dabbelt.com, conor+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, robh+dt@kernel.org, u.kleine-koenig@pengutronix.de, thierry.reding@gmail.com, aou@eecs.berkeley.edu Cc: zong.li@sifve.com, vincent.chen@sifive.com, linux-pwm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, nylon7717@gmail.com, Nylon Chen Subject: [v6 2/3] pwm: sifive: change the PWM controlled LED algorithm Date: Tue, 16 Jan 2024 12:10:53 +0800 Message-ID: <20240116041054.11641-3-nylon.chen@sifive.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20240116041054.11641-1-nylon.chen@sifive.com> References: <20240116041054.11641-1-nylon.chen@sifive.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The `frac` variable represents the pulse inactive time, and the result of this algorithm is the pulse active time. Therefore, we must reverse the result. The reference is SiFive FU740-C000 Manual[0] Link: https://sifive.cdn.prismic.io/sifive/1a82e600-1f93-4f41-b2d8-86ed8b16acba_fu740-c000-manual-v1p6.pdf [0] Co-developed-by: Zong Li Signed-off-by: Zong Li Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Nylon Chen --- drivers/pwm/pwm-sifive.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/pwm/pwm-sifive.c b/drivers/pwm/pwm-sifive.c index eabddb7c7820..b07c8598bb21 100644 --- a/drivers/pwm/pwm-sifive.c +++ b/drivers/pwm/pwm-sifive.c @@ -113,6 +113,7 @@ static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm, u32 duty, val; duty = readl(ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm)); + duty = (1U << PWM_SIFIVE_CMPWIDTH) - 1 - duty; state->enabled = duty > 0; @@ -123,11 +124,10 @@ static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm, state->period = ddata->real_period; state->duty_cycle = (u64)duty * ddata->real_period >> PWM_SIFIVE_CMPWIDTH; - state->polarity = PWM_POLARITY_INVERSED; + state->polarity = PWM_POLARITY_NORMAL; return 0; } - static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm, const struct pwm_state *state) { @@ -139,7 +139,7 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm, int ret = 0; u32 frac; - if (state->polarity != PWM_POLARITY_INVERSED) + if (state->polarity != PWM_POLARITY_NORMAL) return -EINVAL; cur_state = pwm->state; @@ -159,6 +159,7 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm, frac = DIV64_U64_ROUND_CLOSEST(num, state->period); /* The hardware cannot generate a 100% duty cycle */ frac = min(frac, (1U << PWM_SIFIVE_CMPWIDTH) - 1); + frac = (1U << PWM_SIFIVE_CMPWIDTH) - 1 - frac; mutex_lock(&ddata->lock); if (state->period != ddata->approx_period) { -- 2.42.0